Introduction: Why Does JSON in PHP Look Like a Mess?
\n\nImagine 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:
{"users":[{"id":1,"name":"Ivan","email":"ivan@example.com"},{"id":2,"name":"Maria","email":"maria@example.com"}]}\n\nYou'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\nThis 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\nWhat is Pretty-Print and Why Do You Need It?
\n\nPretty-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\nImagine 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\nIn 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.
Method 1: JSON_PRETTY_PRINT — Your Best Friend
\n\nThe 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.
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\nResult:
\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\nMuch better, right? Each array element is now on a separate line, indentation is set, and the structure is intuitively readable.
\n\nCombining with Other Flags
\n\nJSON_PRETTY_PRINT can and should be combined with other flags for more fine-tuning. For example:
- \n
- JSON_UNESCAPED_UNICODE — does not escape Cyrillic and other Unicode characters (instead of
\\u0438\\u0432\\u0430\\u043dyou will seeIvan). \n - JSON_UNESCAPED_SLASHES — does not escape slashes (useful for URLs). \n
- JSON_NUMERIC_CHECK — converts strings containing numbers to a numeric type. \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\nResult:
\n\n{\n "message": "Hello, world!",\n "url": "https://example.com/api/v1/users"\n}\n\nNotice: Cyrillic characters display normally, slashes are not escaped. This is exactly how you typically want to output JSON for a human.
\n\nMethod 2: Formatting an Existing JSON String
\n\nBut what if you already have a JSON string (for example, obtained from a file or a database), and i