Reject malformed JSON

Take your time, follow the code and check your understanding.

The codePHP arrays and requests
<?php
$input = json_decode($raw, true);
// How should malformed JSON be handled?
Your answer4 min · no timer
What needs fixing?

Show solution and explanation

JSON_THROW_ON_ERROR distinguishes malformed JSON from a legitimate JSON null. Validate that a decoded request has the expected object shape.

Use JSON_THROW_ON_ERROR and catch JsonException.