Introduction to JSON and PHP
JSON (JavaScript Object Notation) is a lightweight data interchange format that has become the de facto standard for communication between web servers and clients. In modern PHP web development, you will encounter JSON everywhere: from API requests to configuration files and storing structured data.
In this article, we will cover two fundamental processes of working with JSON in PHP:
- Serialization — converting a PHP array or object into a JSON string (the
json_encode()function). - Parsing (deserialization) — the reverse conversion of a JSON string into a PHP variable (the
json_decode()function).
The material is aimed at beginner PHP developers, so we will progress from simple to complex, with detailed code examples.
Data Serialization: The json_encode() Function
Serialization is the process of converting data from PHP's internal representation (arrays, objects) into a JSON format string. This is necessary when you want to send data to a client (browser, mobile application) or save it to a file/database in a universal format.
Converting an Array to JSON
The simplest case is serializing an associative array:
<?php$user = [ "name" => "Ivan Petrov", "age" => 28, "email" => "ivan@example.com", "is_active" => true];
$jsonString = json_encode($user, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);echo $jsonString;?>Result:
{ "name": "Ivan Petrov", "age": 28, "email": "ivan@example.com", "is_active": true}Note the flags:
JSON_UNESCAPED_UNICODE— preserves Cyrillic characters (without this flag, Russian characters will be converted to \\uXXXX).JSON_PRETTY_PRINT— formats the output with indentation for readability (useful for debugging, but increases the string size).
Converting an Object to JSON
PHP objects can also be serialized. Only public properties will be included:
<?phpclass Product { public $id; public $title; private $secretPrice; // will not be included in JSON
public function __construct($id, $title, $price) { $this->id = $id; $this->title = $title; $this->secretPrice = $price; }}
$product = new Product(1, "Laptop", 75000);echo json_encode($product, JSON_UNESCAPED_UNICODE);// Output: {"id":1,"title":"Laptop"}?>Working with Nested Structures
JSON supports nesting. PHP arrays of any depth are correctly converted:
<?php$data = [ "company" => "TechCorp", "employees" => [ ["name" => "Anna", "position" => "Developer"], ["name" => "Boris", "position" => "Designer"] ], "address" => [ "city" => "Moscow", "street" => "Tverskaya" ]];
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);?>Parsing JSON: The json_decode() Function
Parsing (deserialization) is the reverse process: from a JSON string we obtain data that PHP can work with.
Getting an Object
By default, json_decode() returns an object of the stdClass class:
<?php$jsonString = '{"name":"Ivan Petrov","age":28,"email":"ivan@example.com"}';$user = json_decode($jsonString);
echo $user->name; // Ivan Petrovecho $user->age; // 28echo $user->email; // ivan@example.com?>Getting an Array
If you pass a second argument true, the function returns an associative array:
&l