PHP Tutorials

PHP 8 JSON Data Encode and Decode Examples

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.

  $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.


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:


  $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.


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


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

# output

object(stdClass)#1 (4) {

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.


I am Digamber, a full-stack developer and fitness enthusiast. I crafted this site to bestow my coding experience and love to write on JavaScript, React, Angular, Vue, Laravel.

Recent Posts

React Display Validation Error Messages with Hook Form Tutorial

As a React developer, you must know how to implement validation in form, and a…

2 days ago

React Hook Form 7 Required Checkbox Validation Tutorial

Creating checkboxes in React is not so difficult, especially when you are using React Hook…

5 days ago

React Hook Form 7 Custom Radio Buttons Tutorial

Learning React is not complicated; instead, it's easy, but only when you have the right…

1 week ago

React Bootstrap Custom Dropdown List with Fetch API Tutorial

Dropdown is a valuable and prominent module, which is often used to select one option…

2 weeks ago

React Hook Form Reset / Clear Form Values and Errors Tutorial

React form reset tutorial; Forms are handy provides the best possible way to get information…

2 weeks ago

React Show Loading Spinner on Submit Click Tutorial

You click on the submit button, form is processed, but nothing is being shown to…

2 weeks ago