pretty-print JSON using JavaScript

Cover Image for pretty-print JSON using JavaScript
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

🌟 Easy and Pretty JSON Printing with JavaScript! 📝

Are you tired of staring at ugly, cluttered JSON data? Want to make it easier for human eyes to comprehend? Look no further! In this blog post, we'll address the common issue of pretty-printing JSON using JavaScript and provide you with simple solutions. So let's dive right in! 💪

The Problem 😫

Many times, when working with JSON data, it comes in a compact and unreadable form. The lack of indentation and whitespace makes it difficult for human readers to understand and analyze the data. Additionally, it lacks any visual cues like colors or font styles which could enhance readability.

The Solution 💡

1. JSON.stringify() with the indent parameter

JavaScript provides a built-in method called JSON.stringify() that converts JavaScript objects into JSON strings. By passing an optional second parameter null (or a string specifying the indentation), we can pretty-print the JSON data.

const jsonData = { 
  name: "Techie",
  age: 42,
  skills: ["JavaScript", "Python", "HTML", "CSS"]
};

const prettyJson = JSON.stringify(jsonData, null, 2);
console.log(prettyJson);

In the example above, we create a JavaScript object jsonData and use JSON.stringify() to convert it to a JSON string with an indentation level of 2. The result is a beautifully formatted JSON data that is easily readable.

2. Syntax Highlighting Libraries

If you're looking to take things up a notch and add colors or font styles to your pretty-printed JSON, you can use syntax highlighting libraries like Prism.js or Highlight.js. These libraries provide a simple way to add visual cues and improve the readability of your JSON data.

Here's an example using Prism.js:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/themes/prism.min.css">
  </head>
  <body>
    <pre><code class="language-json">
    {
      "name": "Techie",
      "age": 42,
      "skills": ["JavaScript", "Python", "HTML", "CSS"]
    }
    </code></pre>
    <script src="//cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/prism.min.js"></script>
  </body>
</html>

In this example, we include the necessary CSS and JavaScript files for Prism.js. Then, we wrap our JSON data inside a <code> tag with the appropriate class for JSON highlighting. When the page loads, Prism.js will automatically apply syntax highlighting to the JSON code.

📢 Time to Take Action! 💪

Now that you know how to pretty-print JSON using JavaScript, it's time to put it into practice! Try it out on your own projects and see the difference it makes in readability and ease of understanding.

Share your experiences and any other tips you have on pretty-printing JSON in the comments below! Let's make our JSON data beautiful together! 🌈❤️

Happy coding! 🚀


More Stories

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

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 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

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# 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

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# 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

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# 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

Matheus Mello
Matheus Mello