Table of Contents
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:
- The array to convert to JSON
- 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 itJSON_UNESCAPED_SLASHES
- Don't escape slashesJSON_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!
- Managing PHP Dependencies with Composer
- Getting Started with Svelte
- Git Tutorial: Learn how to use Version Control
- How to Set Up Cron Jobs in Linux
- Best Visual Studio Code Extensions for 2022
- How to deploy an Express app using Docker
- How to deploy a Node app using Docker
- How to Scrape the Web using Node.js and Puppeteer
- Getting User Location using JavaScript's Geolocation API
- Getting Started with Moment.js
- Learn how to build a Slack Bot using Node.js
- Building a Real-Time Note-Taking App with Vue and Firebase