Working with JSON in PHP: Parsing and Serialization for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Working with JSON in PHP

\n\n

JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in web development for transferring information between a client and a server. Despite its origins in JavaScript, JSON has become a universal standard supported by virtually all programming languages, including PHP.

\n\n

In PHP, working with JSON is implemented natively, making processing this format simple and efficient. In this article, we will thoroughly examine how to perform JSON parsing (converting a string into PHP data structures) and serialization (converting PHP data into a JSON string). The material is aimed at beginner developers, so we will progress from simple to complex.

\n\n

By the end of the article, you will learn:

\n
    \n
  • Convert PHP arrays and objects to JSON
  • \n
  • Parse JSON strings into associative arrays and objects
  • \n
  • Handle errors when working with JSON
  • \n
  • Use nested data structures
  • \n
\n\n

Basic Functions for Working with JSON in PHP

\n\n

PHP provides two main functions for working with JSON:

\n\n

json_encode() — Data Serialization

\n\n

The json_encode() function converts a PHP variable (array, object, string, number) into a JSON string. This is called serialization.

\n\n

Basic usage example:

\n\n
<?php\n// Associative array\n$user = [\n    "name" => "Ivan",\n    "age" => 28,\n    "email" => "ivan@example.com",\n    "is_active" => true\n];\n\n$json = json_encode($user);\necho $json;\n// Output: {"name":"Ivan","age":28,"email":"ivan@example.com","is_active":true}\n?>
\n\n

The function automatically converts PHP data types to corresponding JSON types:

\n
    \n
  • Strings → JSON strings
  • \n
  • Numbers → JSON numbers
  • \n
  • Boolean values → true/false
  • \n
  • Arrays → JSON objects or arrays (depends on keys)
  • \n
  • NULL → null
  • \n
\n\n

json_decode() — JSON Parsing

\n\n

The json_decode() function performs the reverse operation — it converts a JSON string into a PHP variable. This is called parsing.

\n\n

Example:

\n\n
<?php\n$jsonString = '{"name":"Ivan","age":28,"email":"ivan@example.com","is_active":true}';\n\n// Parse into an object (default)\n$userObject = json_decode($jsonString);\necho $userObject->name; // Output: Ivan\n\n// Parse into an associative array (second parameter true)\n$userArray = json_decode($jsonString, true);\necho $userArray["name"]; // Output: Ivan\n?>
\n\n

Note the second parameter of the json_decode() function:

\n
    \n
  • false (default) — returns stdClass objects
  • \n
  • true — returns associative arrays
  • \n
\n\n

Advanced Features and Error Handling

\n\n

Working with Nested Structures

\n\n

JSON supports nested objects and arrays. PHP handles such structures easily:

\n\n
<?php\n// Complex data structure\n$data = [\n    "company" => "TechCorp",\n    "employees" => [\n        ["name" => "Anna", "position" => "Developer"],\n        ["name" => "Peter", "position" => "Designer"],\n        ["name" => "Maria", "position" => "Manager"]\n    ],\n    "address" => [\n        "city" => "Moscow",\n        "street" => "Tverskaya",\n        "building" => 10\n    ]\n];\n\n$json = json_encode($data, JSON_PRETTY_PRINT);\necho $json;\n?>
\n\n

The result will be formatted for readability thanks to the JSON_PRETTY_PRINT flag.

\n\n

Error Handling During Parsing

\n\n

When working with external data (API, files), it is important to check that the JSON is valid. Use the json_last_error() function:

\n\n
<?php\n$badJson = '{"name":                

Blogs

Book Recommendations