PHP 8 JSON Data Encode and Decode Examples

Last Updated on by in PHP

This detailed article will ascertain how to swiftly on top of that easily encode and decode the JSON data in PHP.

Before we dive into the JSON Parsing examples, we must understand what JSON is?

Well, JSON means JavaScript Object Notation. It is a quintessential mechanism used for transferring and storing the data.

In general, JSON is employed for sending data from the remote server in object and array form.

JSON is an advanced version of XML; nonetheless, it is easy to create and most importantly.

It is easy to scan for humans and computers. The best thing about JSON is it is lightweight compare than XML.

JSON has two types of structure object and array. The object is invoked by a curly bracket and a set of key/value pair.

You can define comma (,) to separate multiple key-value pairs.

PHP 8 JSON Data Encode Examples

JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data.

These functions are json_encode() and json_decode(), respectively. Both functions only works with UTF-8 encoded string data.

In this quick JSON parsing example, you will learn how to encode a PHP associative array into a JSON object.

Hence, you will use json_encode() function and encode a value to JSON format.

Let’s convert the PHP string to JSON.

<?php
  $fruits = array("Mango"=>95, "Cherry"=>120, "Kiwi"=>100, "Orange"=>55);
 
  echo json_encode($fruits);
?>

In general, the JSON encode function takes three parameters, array, option, depth; having said that, we only passed the array in the below example. It returned the encoded values into the JSON format.

{"Mango":95,"Cherry":120,"Kiwi":100,"Orange":55}

PHP Encode Indexed Array

Suppose you want to know how to encode an indexed array into a JSON array easily. So, use the below code example where we are encoding an indexed array and transforming it into a JSON array:

<?php

  $netflixShows = array("Snowpiercer", "Cursed", "The Last Kingdom", "Titans");

  echo json_encode($netflixShows);

?>

Here is the output, you will get in response to above code example:

["Snowpiercer","Cursed","The Last Kingdom","Titans"]

PHP 8 JSON Data Decode Examples

So far, we have gone through the encoding examples, but now we will go one step further and check out the exemplary methods to decode the JSON data in PHP.

For the next example, we are going to use json_decode() function, and it helps you flawlessly transform or decode a JSON encoded string.

You just need to pass the JSON encoded string as a parameter and convert it into a PHP variable.

<?php

  $jsonString = '{"Mango":95,"Cherry":120,"Kiwi":100,"Orange":55}';
 
  var_dump(json_decode($jsonString));

?>

Here is what the decoding JSON data in PHP output will look like:

# output

object(stdClass)#1 (4) {
  ["Mango"]=>
  int(95)
  ["Cherry"]=>
  int(120)
  ["Kiwi"]=>
  int(100)
  ["Orange"]=>
  int(55)
}

By default the json_decode() function returns an object.

You can optionally specify a second parameter, $assoc, which accepts a Boolean value that, when set as true JSON objects are decoded into associative arrays. It is false by default.

Here’s an example: In the above example, the json decode function returns an object, although it returns an object by default.

Nevertheless, you can pass the $assoc parameter, which is optional and Boolean.

When you set it to true, it will return an object, which will be transformed into an associate array.

Plus, you set it to false, then it will return an object.