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 "Dirty" JSON is Every Developer's Enemy

\n\n

Imagine: 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\n

Your 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\n

In this article, you will learn:

\n
    \n
  • What pretty-printing is and why it's needed
  • \n
  • How to use the built-in json_encode function with flags
  • \n
  • How to format JSON for debugging, logs, and browser output
  • \n
  • Additional techniques: recursive formatting, working with Unicode, and compression
  • \n
\n\n

Let's go!

\n\n

What is Pretty-Printing JSON?

\n\n

Pretty-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\n

Agree, it's much nicer to work with! PHP provides built-in tools for this that don't require installing additional libraries.

\n\n

The Main Method: json_encode with the JSON_PRETTY_PRINT Flag

\n\n

The 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:

\n\n
<?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\n

Result:

\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\n

Note: 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.

\n\n

Combining Flags

\n\n

You can pass multiple flags using | (bitwise OR). For example, to simultaneously format and escape Unicode characters:

\n\n
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);\necho $json;
\n\n

The 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.

\n\n

How to Display Pretty-Printed JSON in the Browser

\n\n

If 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:

\n\n
<?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                

Blogs

Book Recommendations