Introduction: Why "Dirty" JSON is Every Developer's Enemy
\n\nImagine: you're writing an API in PHP, returning data in JSON format, opening the result in a browser or console — and you see one continuous string without a single space:
\n\n{"users":[{"id":1,"name":"Ivan","email":"ivan@example.com"},{"id":2,"name":"Maria","email":"maria@example.com"}]}\n\nYour eyes dart around, finding the right field is impossible, and if the JSON contains an error — you'll be searching for it for hours. Sound familiar? That's why the question "Pretty-Printing JSON with PHP" on Stack Overflow has thousands of views. Today, we'll once and for all figure out how to make JSON readable, structured, and beautiful using PHP.
\n\nIn this article, you will learn:
\n- \n
- What pretty-printing is and why it's needed \n
- How to use the built-in
json_encodefunction with flags \n - How to format JSON for debugging, logs, and browser output \n
- Additional techniques: recursive formatting, working with Unicode, and compression \n
Let's go!
\n\nWhat is Pretty-Printing JSON?
\n\nPretty-printing is formatting data with indentation, line breaks, and alignment so that a person can easily read the structure. In the case of JSON, it turns a "mess" into a neat hierarchy:
\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\nAgree, it's much nicer to work with! PHP provides built-in tools for this that don't require installing additional libraries.
\n\nThe Main Method: json_encode with the JSON_PRETTY_PRINT Flag
\n\nThe simplest and most common method is to use the second parameter of the json_encode() function, passing the constant JSON_PRETTY_PRINT. Here's a basic example:
<?php\n$data = [\n 'name' => 'PythonLib',\n 'type' => 'educational',\n 'topics' => ['PHP', 'Python', 'JavaScript'],\n 'stats' => [\n 'visitors' => 15000,\n 'rating' => 4.8\n ]\n];\n\n$prettyJson = json_encode($data, JSON_PRETTY_PRINT);\necho $prettyJson;\n?>\n\nResult:
\n\n{\n "name": "PythonLib",\n "type": "educational",\n "topics": [\n "PHP",\n "Python",\n "JavaScript"\n ],\n "stats": {\n "visitors": 15000,\n "rating": 4.8\n }\n}\n\nNote: keys are sorted in alphabetical order. If you need to preserve the original order — use the JSON_PRESERVE_ZERO_FRACTION flag (for floating-point numbers) or sort the array manually.
Combining Flags
\n\nYou can pass multiple flags using | (bitwise OR). For example, to simultaneously format and escape Unicode characters:
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);\necho $json;\n\nThe JSON_UNESCAPED_UNICODE flag preserves Cyrillic and other characters in a readable form, rather than as \\u0438\\u0432\\u0430\\u043d. This is especially useful for Russian-language websites.
How to Display Pretty-Printed JSON in the Browser
\n\nIf you're developing an API and want JSON to display beautifully directly in the browser (for example, during debugging), use the Content-Type: application/json header along with formatting:
<?php\nheader('Content-Type: application/json; charset=utf-8');\n\n$response = [\n 'status' => 'success',\n 'data' => [\n 'message' => 'Hello, world!',\n 'code' => 200\n ]\n];\n\necho