JSON encoding and decoding lets you turn PHP data into a string format that computers can share easily, and then turn it back into PHP data.
JSON encoding and decoding in PHP
$jsonString = json_encode($phpData); $phpData = json_decode($jsonString, true);
json_encode() converts PHP data (like arrays or objects) into a JSON string.
json_decode() converts a JSON string back into PHP data. Use true as the second argument to get an array instead of an object.
$data = ['name' => 'Anna', 'age' => 25]; $json = json_encode($data); echo $json;
$json = '{"name":"Anna","age":25}'; $data = json_decode($json, true); print_r($data);
true returns an object. You can access properties with ->.$object = json_decode($json); echo $object->name;
This program shows how to encode a PHP array into JSON and then decode it back to a PHP array.
<?php // Create PHP array $user = ['username' => 'johndoe', 'email' => 'john@example.com', 'active' => true]; // Convert PHP array to JSON string $jsonString = json_encode($user); echo "JSON string:\n" . $jsonString . "\n\n"; // Convert JSON string back to PHP array $decodedArray = json_decode($jsonString, true); echo "Decoded PHP array:\n"; print_r($decodedArray); ?>
JSON only supports certain data types: strings, numbers, arrays, objects, booleans, and null.
When decoding, use true as the second argument to get an associative array instead of an object.
Always check if json_encode() or json_decode() returns false to catch errors.
Use json_encode() to turn PHP data into a JSON string.
Use json_decode() to turn a JSON string back into PHP data.
JSON is a simple way to share data between programs and languages.
Practice
json_encode() do?Solution
Step 1: Understand the purpose of
This function takes PHP variables like arrays or objects and converts them into a JSON string format.json_encode()Step 2: Differentiate from
json_decode()json_decode()does the opposite by converting JSON strings back to PHP data.Final Answer:
Converts PHP data into a JSON formatted string -> Option AQuick Check:
json_encode() = PHP to JSON string [OK]
- Confusing json_encode() with json_decode()
- Thinking json_encode() reads JSON from files
- Assuming json_encode() validates JSON syntax
Solution
Step 1: Identify the function to decode JSON
The function to convert JSON string to PHP data isjson_decode().Step 2: Use the second parameter to get an array
Passingtrueas the second argument returns an associative array instead of an object.Final Answer:
$data = json_decode($jsonString, true); -> Option BQuick Check:
json_decode() with true returns array [OK]
- Using json_encode() instead of json_decode()
- Omitting the second parameter to get an array
- Passing true to json_encode() which is invalid
$arr = ['name' => 'Anna', 'age' => 25]; $json = json_encode($arr); echo $json;
Solution
Step 1: Understand json_encode output
The function converts the PHP associative array into a JSON string with keys and values in double quotes.Step 2: Check the exact JSON string format
The output is a JSON object string: {"name":"Anna","age":25} with escaped quotes when printed.Final Answer:
{"name":"Anna","age":25} -> Option AQuick Check:
json_encode() outputs JSON string [OK]
- Expecting PHP array print instead of JSON string
- Missing quotes around keys and values
- Confusing output with plain text or error
$json = '{"fruit":"apple","count":3}';
$data = json_decode($json, true);
echo $data->fruit;Solution
Step 1: Check json_decode parameters
The second parametertruemakes json_decode return an associative array.Step 2: Accessing array with object syntax
Using$data->fruittries to access an object property, but $data is an array, so this causes an error.Final Answer:
Using object syntax on an array after json_decode -> Option DQuick Check:
json_decode(true) returns array, use $data['fruit'] [OK]
- Accessing array as object after json_decode(true)
- Assuming JSON string is invalid
- Forgetting to pass true for array output
$data = ["id" => 101, "tags" => ["php", "json", "coding"]];
Which code correctly encodes this array to JSON and then decodes it back to a PHP object?
Solution
Step 1: Encode PHP array to JSON string
Usejson_encode($data)to convert the PHP array into a JSON string.Step 2: Decode JSON string back to PHP object
Callingjson_decode($json)without the second parameter returns a PHP object.Final Answer:
$json = json_encode($data); $obj = json_decode($json); -> Option CQuick Check:
Encode then decode without true returns object [OK]
- Using json_decode before json_encode
- Passing true to json_encode (invalid)
- Decoding with true returns array, not object
Ready to prove it?
Pass the test and this pattern moves unknown
