How to Pretty Print JSON in PHP

Updated onbyAlan Morel
How to Pretty Print JSON in PHP

PHP remains one of the most popular programming languages for web development because of how easy and straightforward it is to work with.

JSON is another popular technology, a data format that is used to transmit data between a server and a client.

In this post, we'll learn how to pretty print JSON in PHP.

How to Pretty Print JSON from an Array in PHP

To pretty print JSON in PHP, first, let's define an array that we want to convert to JSON:

PHP
$data = array( 'name' => 'John', 'age' => 30, 'cars' => array( 'Ford', 'BMW', 'Fiat' ) );

Now we can use the json_encode() function to convert the array to JSON.

This function takes two parameters:

  1. The array to convert to JSON
  2. A bitmask that specifies how the JSON should be formatted

Some of the most common bitmasks are:

  • JSON_PRETTY_PRINT - Use whitespace in returned data to format it
  • JSON_UNESCAPED_SLASHES - Don't escape slashes
  • JSON_UNESCAPED_UNICODE - Encodes multi-byte unicode characters literally

In our case, we want to use the JSON_PRETTY_PRINT bitmask to pretty print the JSON:

PHP
$data = array( 'name' => 'John', 'age' => 30, 'cars' => array( 'Ford', 'BMW', 'Fiat' ) ); $json = json_encode($data, JSON_PRETTY_PRINT);

Now that you have your JSON, you can print it to the screen by wrapping it with pre tags:

PHP
$data = array( 'name' => 'John', 'age' => 30, 'cars' => array( 'Ford', 'BMW', 'Fiat' ) ); $json = json_encode($data, JSON_PRETTY_PRINT); echo $json;
JSON
{ "name": "John", "age": 30, "cars": [ "Ford", "BMW", "Fiat" ] }

How to Pretty Print JSON from a String in PHP

If you have a JSON string, you can use the json_decode() function to convert it to an array.

Once you have this array, you can then repeat the steps above to pretty print the JSON.

Let's first define a JSON string:

PHP
$raw = '{ "name": "John", "age": 30, "cars": [ "Ford", "BMW", "Fiat" ] }';

Now, let's convert the JSON string to an array:

PHP
$raw = '{ "name": "John", "age": 30, "cars": [ "Ford", "BMW", "Fiat" ] }'; $data = json_decode($raw, true);

Now we can use the json_encode() function to convert the array to JSON:

PHP
$raw = '{ "name": "John", "age": 30, "cars": [ "Ford", "BMW", "Fiat" ] }'; $data = json_decode($raw, true); $json = json_encode($data, JSON_PRETTY_PRINT);

Finally, we can print this JSON to the screen:

PHP
$raw = '{ "name": "John", "age": 30, "cars": [ "Ford", "BMW", "Fiat" ] }'; $data = json_decode($raw, true); $json = json_encode($data, JSON_PRETTY_PRINT); echo $json;
JSON
{ "name": "John", "age": 30, "cars": [ "Ford", "BMW", "Fiat" ] }

Conclusion

In this post, we learned how to pretty print JSON in PHP.

You can use either an array or a JSON string, and then use the json_encode() function to convert it to JSON.

Thanks for reading!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.