Limited Offer
60% OFF on all plans!

JSON in PHP

Learn how to encode PHP arrays and objects to JSON strings and decode JSON strings back using json_encode() and json_decode().

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight, text-based format for representing structured data. Despite its name, it is language-independent and is used everywhere — REST APIs, config files, databases, and inter-service communication. PHP's `json_encode()` and `json_decode()` handle the conversion to and from JSON.

1. What is JSON?

JSON is a text format for structured data, universal across all languages.

JSON Data Types

JSON supports exactly six value types. Knowing how they map to PHP types is essential — some PHP types (like `resource`, `float INF`, `NAN`) cannot be represented in JSON.

1. Supported JSON Data Types

string, number, boolean, null, array, and object.

JSON Rules

JSON has strict syntax rules. Violating any of them makes the entire string invalid. PHP's `json_last_error()` helps identify what went wrong.

1. Important JSON Rules

Keys must be double-quoted strings; trailing commas and comments are not allowed.

json_encode() — PHP to JSON

`json_encode($value, $flags)` converts any PHP value into its JSON string representation. The optional flags argument controls formatting and encoding behavior.

1. Basic json_encode()

Convert a PHP array or object to a JSON string.

2. Pretty Printing with JSON_PRETTY_PRINT

Add indentation for human-readable output.

3. Useful json_encode() Flags

Control Unicode escaping, slashes, and numeric checks.

json_decode() — JSON to PHP

`json_decode($json, $associative)` converts a JSON string into a PHP value. By default it returns `stdClass` objects, but passing `true` as the second argument returns associative arrays instead — which is usually more practical.

1. Basic json_decode()

Parse JSON into a stdClass object (default) or associative array.

2. Decoding Nested JSON

Access deeply nested values after decoding.

JSON Error Handling

JSON parsing can fail if the input is malformed. PHP provides `json_last_error()` and the `JSON_THROW_ON_ERROR` flag (PHP 7.3+) to detect and handle errors.

1. Safe Parsing with JSON_THROW_ON_ERROR — PHP 7.3+

Throw a JsonException on failure instead of silently returning null.

2. Legacy Error Checking with json_last_error()

Check for errors after encode/decode without exceptions (pre-PHP 7.3 style).

Deep Cloning with JSON

Encoding a value to JSON and decoding it back is a simple way to create a completely independent deep copy. PHP 8.1+ also offers the native `clone` keyword for objects.

1. Deep Clone via JSON Round-Trip

Encode then decode to produce a fully independent copy.

JSON Validation

Always validate JSON from external sources — APIs, user input, files — before using it in your application.

1. Basic JSON Validation

Use json_validate() (PHP 8.3+) or decode + check error.

2. Safe JSON Parse Helper (All PHP Versions)

A reusable helper that validates and decodes JSON safely.

3. Real-World: Decoding an API Response

Parse JSON from an API, validate it, and access the data.