Pretty-Printing JSON with PHP

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Pretty-Printing JSON with PHP

💻📝 Pretty-Printing JSON with PHP: Making Your Data Look Beautiful 😍🌈

If you've ever worked with JSON data in PHP, you might have encountered the issue of your JSON output looking less than pretty. 🤔😕

You know, the kind of JSON that looks like a tangled mess of characters, making it hard to read and understand. 😫🙈

Well, fear not! 🥳 In this blog post, we're going to address this common issue and provide you with easy solutions to pretty-print your JSON output using PHP. 🎉🙌

So, let's get started! 🚀

Understanding the Problem

To understand the issue, let's take a look at an example script that outputs JSON data:

$data = array('a' => 'apple', 'b' => 'banana', 'c' => 'catnip');
header('Content-type: application/json');
echo json_encode($data);

The output of this script looks like this:

{"a":"apple","b":"banana","c":"catnip"}

Now, this might be fine for small amounts of data, but when you're dealing with larger JSON structures, it quickly becomes unreadable. 😱

The Solution: JSON_PRETTY_PRINT

Thankfully, PHP provides an easy solution for pretty-printing JSON. 🙌🎉

When encoding your JSON, you can use the JSON_PRETTY_PRINT flag as a parameter to json_encode(). This flag formats the JSON output in a more human-readable way, adding indentation and line breaks. 😍🌈

Here's the updated code:

$data = array('a' => 'apple', 'b' => 'banana', 'c' => 'catnip');
header('Content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);

And voila! 🎉🌟 The output will now look like this:

{
    "a": "apple",
    "b": "banana",
    "c": "catnip"
}

The Facebook Solution 🚀

You might be wondering if PHP can pretty-print JSON without the JSON_PRETTY_PRINT flag. Well, it turns out that someone at Facebook found a cute little hack to achieve this. 😎💡

By setting the pre tag as the content-type, PHP will format the JSON as if it were preformatted text, resulting in a pretty-printed output. 😏💪

Here's the modified code:

$data = array('a' => 'apple', 'b' => 'banana', 'c' => 'catnip');
header('Content-type: text/html');
echo '<pre>' . json_encode($data) . '</pre>';

And guess what? 🙌🌈 The output will look just as pretty as before!

{
    "a": "apple",
    "b": "banana",
    "c": "catnip"
}

Your Turn! 🎉

Now that you know how to pretty-print JSON with PHP, it's time to give it a try in your own projects! 🚀✨

No more tangled mess of characters – make your JSON data shine! 😍💫

Link to your own examples or share your experiences in the comments section below. We'd love to see what you come up with! 📩🗒️

Happy coding! 💻🌟

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my