How to encode and decode JSON data using php

Updated: 14-Jan-2022 | Tags: PHP and JSON | Views: 895


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.

  • 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'];

We see that the encoded output, is an array like structured string.

["George", "Betty", "Tony", "Tania"]

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.


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"
Here are all the flags that we can use with the json_encode function.

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.


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',
This applies only when the JSON string is an object. If we have a JSON string structured as an array, the json_decode function will return an array even if the JSON_OBJECT_AS_ARRAY flag is added.

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',
Here are all the flags that we can use with the json_decode function.


  • 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 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 docs for json_decode.

Buy me a coffee

If you like to say thanks, you can buy me a coffee.

Buy me a coffee with paypal

Comment section

You can leave a comment, it will help me a lot.

Or you can just say hi. 😉

Tutorial Categories