Formats and protocols · Question 10 of 12
The response of an API should be processed with json_decode(), and invalid JSON should not go unnoticed. Which call fits?
1 answer is correct. Medium
Show answer
Answer
- B json_decode($body, true, 512, JSON_THROW_ON_ERROR)
Explanation
Without JSON_THROW_ON_ERROR, json_decode() returns null for invalid JSON, which cannot be distinguished from a valid "null", and the reason would have to be requested with json_last_error(). With the flag PHP throws a JsonException with an error message. json_decode() never returns false on errors. The second parameter is a bool for associative arrays, not flags.