json
  1. php-json-introduction

PHP JSON Introduction

JSON (JavaScript Object Notation) is a popular data format used for exchanging data between a web server and a web client.

PHP has inbuilt functions that can be used to convert data to JSON and back from JSON to PHP.

Syntax

Encoding Data to JSON

The syntax to encode data to JSON in PHP is:

json_encode($data, $options, $depth);
  • $data: The data to be encoded to JSON.
  • $options (optional): Options to customize the encoding process. It can be a bitmask consisting of the JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_NUMERIC_CHECK, JSON_FORCE_OBJECT, and JSON_HEX_TAG constants.
  • $depth (optional): The maximum nesting depth.

Decoding JSON Data to PHP

The syntax to decode JSON data to PHP in PHP is:

json_decode($jsonString, $assoc, $depth, $options);
  • $jsonString: The JSON data string to be decoded.
  • $assoc (optional): A boolean value indicating whether the returned object should be converted to an associative array or left as an object.
  • $depth (optional): The maximum nesting depth.
  • $options (optional): Options to customize the decoding process. It can be a bitmask consisting of the JSON_BIGINT_AS_STRING constant.

Example

Encoding Data to JSON

The following example encodes an array containing a person's name, age, and location to JSON.

$person = array(
  'name' => 'John Doe',
  'age' => 30,
  'location' => 'USA'
);

$json = json_encode($person);

echo $json;

Output:

{"name":"John Doe","age":30,"location":"USA"}

Decoding JSON Data to PHP

The following example decodes a JSON data string to a PHP object.

$json = '{"name":"John Doe","age":30,"location":"USA"}';

$person = json_decode($json);

echo $person->name; // Output: John Doe

Explanation

  • json_encode() function serializes a PHP value into JSON format.
  • json_decode() function decodes a JSON string to a PHP value.
  • The second argument to json_decode() function is a boolean value indicating whether the returned object should be converted to an associative array or left as an object.
  • The third argument to both json_encode() and json_decode() functions is the maximum nesting depth of the resulting JSON string or PHP object.
  • The fourth argument to json_decode() function is a bitmask of JSON decode options.

Use

  • To exchange data between a web server and a web client in JSON format.
  • To store and manipulate data in JSON format.

Important Points

  • JSON format is preferred over other data formats because of its simplicity and compatibility with a wide range of web-based technologies.
  • PHP provides in-built functions to encode and decode data in JSON format.
  • JSON values can be of the following types: string, number, object, array, boolean, or null.

Summary

In this article, we learned about encoding and decoding data in JSON format using PHP's json_encode() and json_decode() functions, respectively. We also covered the syntax, examples, explanation, use cases, and some important points related to working with JSON in PHP.

Published on: