How to use JSON in PHP?

How to use JSON in php
How to use JSON in PHP?

JSON is the lightweight, data-interchange format. JSON can be stored in .json and being lightweight, it can be transferred OR stored without consuming much memory. In this tutorial, we are going to see How to use JSON in PHP using different functions.

Let’s understand JSON using simple example.

                             { "key" : "value" }

As seen in the above basic syntax, JSON in PHP is a key-value pair string. It starts with a curly bracket followed by a string. This string basically represents the key. After that colon to separate key and its respective value. finally, a value can be of any data type like string, integer, boolean, array, object, or null.

Advantages of JSON:

  1. Lightweight.
  2. Faster.
  3. easy to understand and use.
  4. Easy to generate and parse.
  5. Completely language independent

Misconception – Is JSON String OR Object?

JSON is neither a string nor an object, rather it’s a standard. Well to understand this let’s understand how JSON plays an important role while communicating server and client.

When we send JSON from client to server then we send it in the form of string. As soon as the server receives that JSON string then the server (PHP) convert it into an array using a function json_decode(). This is an in-built function of PHP to convert project string JSON into an array.

And when we send JSON from server to client as a response then it is received as a raw string which is then parsed using JSON.parse javascript function.

Now, we’ll see our main topic-

How to use JSON in PHP

To use JSON in PHP, two main functions plays an important role. So let’s see them one by one.

Reading JSON using PHP

To read JSON data, PHP has provided an in built function- json_decode()

There are mainly two ways of reading JSON string –

PHP read JSON string as an Object

This function (json_decode()) basically converts a JSON string into an object. It takes JSON string as a parameter and converts it into an object. An object, mean stdClass object.

<?php

$data = '
{
   "website" : "https://programmingdive.com/",
   "purpose" : "Knowledge Sharing" 
}
';

Now, we’ll use json_decode() function to decode it and convert it into an object

<?php

$result = json_decode($data);
// OUTPUT

stdClass Object
(
   [website] => https://programmingdive.com/
   [purpose] => Knowledge Sharing
)

We can access any from this object using ->

<?php
echo $result->website;
// OUTPUT

https://programmingdive.com/

PHP read JSON string as an Array

We will use the same above example and convert it into an Array. For this, we just need to add a flag (true) as a second parameter into json_decode() function and that will convert our JSON string into an associative array.

<?php

$result = json_decode($data, true);
// OUTPUT

Array
(
   [website] => https://programmingdive.com/
   [purpose] => Knowledge Sharing
)

Converting an Object / Array into JSON string

We have just seen how to convert a JSON string into an Object / Array. Likewise, we can also convert An Object / Array into a JSON string. For this, we can use json_encode() function.

Converting an Object into JSON String

To convert an Object into a JSON String, we need one object. For this, we’ll create a stdClass for example purpose. In real world, this might be a different or big Object.

<?php

$author = new stdClass;
$author->website = "https://programmingdive.com/";
$author->purpose = "Knowledge Sharing";

$result = json_encode($author);
// OUTPUT

{
   "website": "https://programmingdive.com/",
   "purpose": "knowledge sharing"
}

Converting an Array into JSON String

To convert an array into JSON string, we need to pass an array as a parameter to json_encode() and it will return JSON string.

<?php

$data = array
(
 "website" => "https://programmingdive.com/",
 "purpose" => "Knowledge Sharing"
);

$result = json_encode($data);
// OUTPUT

{
   "website": "https://programmingdive.com/",
   "purpose": "Knowledge Sharing"
}

PHP read JSON file

JSON file always comes with an extension of .json and media type as application/json. To read JSON file we can use file_get_contents() function and once we get all the content of the file then we can parse it using json_decode() function.

<?php

 // path of the JSON file
$file = 'data.json';

// Read the contents of the file and store it into a variable
$data = file_get_contents($file);

  // decode the JSON data
$result = json_decode($data);

In the above basic example, we have read data.json file using file_get_contents() function as string and decoded json string into an object using json_decode() function.

file_get_contents() is also responsible for reading data from URL. We just need to pass the complete URL of the json file and rest of the code will remain as it is. Sometimes we may get an error when trying to access https:// URL as Warning: file_get_contents(): Unable to find the wrapper "https". But we solve this problem by enabling openssl extension in php.ini file

Conclusion:

JSON is very important way of data communication. It saves a memory and because of its lightweight property it’s very famous among the developer community. JSON in PHP has started support from version 5 itself. JSON is mainly used in the client to sever communication because whenever we need to communicate between them we need it to be done in less time and this will be only possible if the sending data is lightweight and of course JSON win then battle in this.