We can pretty print JSON String in PHP using the json_encode() function and by making use of the JSON_PRETTY_PRINT option along with it.
Let's take a look by an example.
Unformatted JSON String
PHP Code
<?php
$unformattedCityJSON = '{"countries":[{"name":"Japan","cities":[{"name":"Tokyo","area":"Kanto"},{"name":"Osaka","area":"Kansai"}]},{"name":"China","cities":[{"name":"Beijing","area":"Beijing Municipality"},{"name":"Shanghai","area":"Shanghai Municipality"}]},{"name":"Australia","cities":[{"name":"Sydney","area":"New South Wales"},{"name":"Melbourne","area":"Victoria"}]}]}';
$data = json_decode($unformattedCityJSON, true);
$prettifiedCityJSON = json_encode($data, JSON_PRETTY_PRINT);
echo $prettifiedCityJSON;
?>
Prettified JSON Output:
{
"countries": [
{
"name": "Japan",
"cities": [
{
"name": "Tokyo",
"area": "Kanto"
},
{
"name": "Osaka",
"area": "Kansai"
}
]
},
{
"name": "China",
"cities": [
{
"name": "Beijing",
"area": "Beijing Municipality"
},
{
"name": "Shanghai",
"area": "Shanghai Municipality"
}
]
},
{
"name": "Australia",
"cities": [
{
"name": "Sydney",
"area": "New South Wales"
},
{
"name": "Melbourne",
"area": "Victoria"
}
]
}
]
}
Example Screenshot
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to PHP,
- Delete file using PHP code : unlink()
- PHP header location function not called
- 403 forbidden error for Image
- Call PHP function on Button click using jquery ajax
- How to Pretty Print JSON in PHP
- Step-by-Step Guide: How to Fix - Error Establishing a Database Connection in WordPress
- PHP Base64 encoding decoding a variable
- PHP Fatal error : Call to a member function bind_param() on a non-object
- PHP.ini: How to Remove URL Forward Slash Before Single or Double Quotes
- PHP drag and drop file upload tutorial using dropzone.js
- Upload Pdf file using PHP Script
- PHP Warning: Cannot modify header information - headers already sent
- macOS - cannot calculate MAC address: Using fd 9 hv_vm_create HV_ERROR [PHP XAMPP]
- PHP Code for sending Emails
- How to destroy PHP session()
- Installing vue.js in Laravel 8
- PHP 301 Redirect Permanently
- Upload docx file using PHP script
- Save current timestamp in MySQL using PHP mysqi binding
- Copy file from one directory to other in Php
- Failed to load resource: net::ERR_CACHE_MISS PHP
- PHP Script to Upload Images to Server
More Posts:
- Add blank lines after each lines using Notepad++ text editor - NotepadPlusPlus
- Android : Unable to load VM from snapshot : Mac OS X Error - Android
- How to send SMS message in Android Emulator - Android
- 3 Python program to add two numbers - Python
- Fix: Unable to edit text in TextEdit on Mac - MacOS
- How to create MD5 digest in Notepad++ - NotepadPlusPlus
- Ways to Show Git Logs in better way using pretty formatting - Git
- Have you noticed MacOS Ventura About this Mac look like Mac OS X - Mac-OS-X