How to encode and decode JSON data using php
Updated: 14-Jan-2022 / Tags: PHP and JSON / Views: 1130 - Author: George
Introduction
Hello everyone, in this post we are going to deal with JSON and PHP.
We are going to see how to encode and decode JSON values
using the php json_encode and json_decode functions.
What is JSON
- JSON stands for JavaScript Object Notation.
- With JSON we can store and transport data over the internet and across different programming languages.
- Almost every programming language has built in functions to read and store JSON data.
- JSON is a text format, when we convert a php array to JSON, basically we creating a string with a javascript object like structure.
Encoding data with json_encode
Let's see an example of an associative array encoded in a JSON format.
We have here a $car array with keys and values.
$car = [
"name" => "BMW",
"color" => "white",
"doors" => 5,
"price" => "19000"
];
This is how the $car array looks like in JSON. It is a string structured as a javascript object.
Now in order to turn an array to JSON we have to use a php function called json_encode(), and we have to pass in the data we want to encode, in our case the $car array.
json_encode($car);
- The json_encode function returns an encoded JSON string on success or false on failure.
- The json_encode function takes as an argument any php data type string, int, array, object, etc except a resource.
- When we encode an associative array the JSON output will always be an object, like the above $car array.
- We can pass as a second argument flags. Those flags era affecting the encoded output.
In example there is a flag that structures the encoded result in a more readable way. That's the JSON_PRETTY_PRINT flag.
json_encode($car, JSON_PRETTY_PRINT);
If we are storing the data in a file, this is how the data would look like. Now we can see much clearer the encoded object, and understand with what we are dealing with.
This is how JSON_PRETTY_PRINT outputs the encoded data.
{ "name": "BMW", "color": "white", "doors": 5, "price": "19000" }
Lets now encode an indexed array of names, and see what results we get.
$names = ['George', 'Betty', 'Tony', 'Tania'];
json_encode($names);
We see that the encoded output, is an array like structured string.
We can force the json_encode function to output an object, with the
JSON_FORCE_OBJECT Flag.
We can combine two flags together with the pipe (|) character.
json_encode($names, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
We see now that our array is encoded to an object, but remember it is a string, structured as an object.
{ "0": "George", "1": "Betty", "2": "Tony", "3": "Tania" }
JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR
Decoding data with json_decode
When we need to read JSON data we are using the json_decode function.
The json_decode function decodes the json string, and returns it as an php object,
or as an array. It depends on the flag that we are using with the function. We will see that in the examples that follows.
Lets say we have a variable that holds a JSON string that we get from a file.
$json = {"name": "BMW", "color": "white", "doors": 5, "price": "19000"};
In this format, the properties and values are not useful to us. We have to decode them to a data type that php can handle. So we use the json_decode function and pass in the $json variable.
json_decode($json);
This will output a php object.
<?php (object) array( 'name' => 'BMW', 'color' => 'white', 'doors' => 5, 'price' => '19000', );?>
If we want the json_decode function to return an array, we have to add the JSON_OBJECT_AS_ARRAY flag,
json_decode($json, JSON_OBJECT_AS_ARRAY);
or we can just say true.
json_decode($json, true);
This will output an array
<?php array ( 'name' => 'BMW', 'color' => 'white', 'doors' => 5, 'price' => '19000', );?>
Lets see an example.
Lets say that we get from a file a json string structured as an array, and want decode
it as an object.
$json = ["George", "Betty", "Tony", "Tania"];
json_decode($json, JSON_OBJECT_AS_ARRAY);
We can not decode an indexed array as an object, the function will return an associative array
<?php array ( 0 => 'George', 1 => 'Betty', 2 => 'Tony', 3 => 'Tania', );?>
JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR.
Summary
- The json_encode function encodes any php data type except a recourse.
- When we encode an associative array the JSON output will always be an object.
- We can add flags to the function to affect the encoded output.
- See the php.net docs for json_encode.
- The json_decode function decodes the json string, and returns it as an php object, or as an array.
- When we add the JSON_OBJECT_AS_ARRAY flag or the keyword true, the returned decoded type is an array, else we get an object.
- See the php.net docs for json_decode.
Comment section
You can leave a comment, it will help me a lot.
Or you can just say hi. 😉