How to Pretty-Print JSON in PHP: A Complete Guide 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: Why Does JSON in PHP Look Like a Mess?

\n\n

Imagine this situation: you wrote a PHP script that receives data from an API or generates a response for the frontend. You output the result using echo json_encode($data) — and get something like this in the browser or console:

\n\n
{"users":[{"id":1,"name":"Ivan","email":"ivan@example.com"},{"id":2,"name":"Maria","email":"maria@example.com"}]}
\n\n

You'll agree, reading this string without formatting is like trying to decipher a doctor's handwriting. Especially when there is a lot of data and deep nesting. This is exactly where pretty-printing JSON in PHP comes to the rescue.

\n\n

This article is for those who are just starting to work with PHP and JSON. We'll break down how to make JSON output beautiful, readable, and convenient for debugging. You'll learn not only the basic methods but also useful life hacks that will save you hours of frustration. Let's go!

\n\n

What is Pretty-Print and Why Do You Need It?

\n\n

Pretty-print (literally — "beautiful printing") is formatting data with indentation, line breaks, and alignment. In the case of JSON, this means that instead of one long string, you get a structured output where each key and value is in its place.

\n\n

Imagine you are debugging an API request or checking a configuration file. Without pretty-print, you'll spend a lot of time trying to figure out where one object ends and another begins. With formatting, everything is clear as day.

\n\n

In PHP, there are several ways to achieve beautiful JSON output. We'll start with the simplest and most popular one — using the JSON_PRETTY_PRINT flag.

\n\n

Method 1: JSON_PRETTY_PRINT — Your Best Friend

\n\n

The easiest way to pretty-print JSON in PHP is to use the JSON_PRETTY_PRINT constant when calling the json_encode() function. This constant is available starting from PHP 5.4.0.

\n\n

Basic Example

\n\n
<?php\n$data = [\n    'users' => [\n        ['id' => 1, 'name' => 'Ivan', 'email' => 'ivan@example.com'],\n        ['id' => 2, 'name' => 'Maria', 'email' => 'maria@example.com']\n    ]\n];\n\n$json = json_encode($data, JSON_PRETTY_PRINT);\necho $json;\n?>
\n\n

Result:

\n\n
{\n    "users": [\n        {\n            "id": 1,\n            "name": "Ivan",\n            "email": "ivan@example.com"\n        },\n        {\n            "id": 2,\n            "name": "Maria",\n            "email": "maria@example.com"\n        }\n    ]\n}
\n\n

Much better, right? Each array element is now on a separate line, indentation is set, and the structure is intuitively readable.

\n\n

Combining with Other Flags

\n\n

JSON_PRETTY_PRINT can and should be combined with other flags for more fine-tuning. For example:

\n\n
    \n
  • JSON_UNESCAPED_UNICODE — does not escape Cyrillic and other Unicode characters (instead of \\u0438\\u0432\\u0430\\u043d you will see Ivan).
  • \n
  • JSON_UNESCAPED_SLASHES — does not escape slashes (useful for URLs).
  • \n
  • JSON_NUMERIC_CHECK — converts strings containing numbers to a numeric type.
  • \n
\n\n
<?php\n$data = [\n    'message' => 'Hello, world!',\n    'url' => 'https://example.com/api/v1/users'\n];\n\n$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);\necho $json;\n?>
\n\n

Result:

\n\n
{\n    "message": "Hello, world!",\n    "url": "https://example.com/api/v1/users"\n}
\n\n

Notice: Cyrillic characters display normally, slashes are not escaped. This is exactly how you typically want to output JSON for a human.

\n\n

Method 2: Formatting an Existing JSON String

\n\n

But what if you already have a JSON string (for example, obtained from a file or a database), and i

Blogs

Book Recommendations