Decoding a JSON string
From JSON string to PHP object: json_decode()
If you have a string (loaded from a file, or retrieved by a API call) that is in JSON format, you can parse the string and transform it in an associative array or in an object.
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).
The function json_decode()
has one mandatory parameter the JSON string and returns the object by default (playing with 2 other optional parameters you can obtain the associative array).
If you have a JSON string that represents an object with name and age attributes:
If you want to obtain a object:
if you want to obtain an associative array:
With the second parameter you can manage the type of the output, use false
if you want an object, use true
if you want an associative array.
Flags
You have a third parameter flags
.
With Flags parameter you can control some behavior of the parsing process.
Flag allowed are:
Constant | Meaning |
---|---|
JSON_BIGINT_AS_STRING | it decodes large integers as their original string value instead of float |
JSON_OBJECT_AS_ARRAY | It decodes JSON objects as PHP array |
JSON_INVALID_UTF8_IGNORE | It ignores invalid UTF-8 characters |
JSON_INVALID_UTF8_SUBSTITUTE | It converts invalid UTF-8 characters to the Unicode characher โreplacement characterโ \0xfffd |
JSON_THROW_ON_ERROR | It throws JsonException if an error occurs |
Catching errors in parsing
My suggestion is to use JSON_THROW_ON_ERROR
in a try catch block:
Big integers
If you have big integers and you want to convert them in a string format instead of the default (float):
More than one flag
If you need to specify more than one flag you can use bit mask operator |
.