Python or JavaScript Pretty-Print JSON

About JSON file

JSON stands for JavaScript Object Notation, and it is a lightweight, text-based data interchange format. JSON files are used to store and exchange data between different systems, languages, and platforms.

A JSON file consists of a collection of key-value pairs, where the keys are always strings and the values can be strings, numbers, arrays, objects, or the special values ‘true‘, ‘false‘, and ‘null‘. The keys and values are separated by a colon ‘:‘ character, and each key-value pair is separated by a comma ‘,‘. The entire JSON file is enclosed in curly braces ‘{}‘.

Here is an example of a simple JSON file that represents a person’s information:

JSON
{
  "name": "John Doe",
  "age": 30,
  "email": "[email protected]",
  "isMarried": true,
  "hobbies": ["reading", "traveling", "cooking"],
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": "12345"
  }
}

JSON files are commonly used in web applications to exchange data between the server and the client. They are also used in many other contexts, such as mobile apps, IoT devices, and data analytics.

What is “Pretty-print”

Pretty-print is the process of formatting text in a way that makes it more readable and visually appealing. In the context of programming and data exchange, pretty-print refers to formatting data structures such as JSON or XML in a way that is easier for humans to read and understand.

When a data structure is pretty-printed, it is typically indented and each element is placed on a new line, making it easier to identify the structure and hierarchy of the data. This can be especially helpful when dealing with large or complex data structures that might be difficult to parse at a glance.

For example, here is a JSON data structure that has not been pretty-printed:

JSON
{"name":"John","age":30,"city":"New York"}

And here is the same data structure after it has been pretty-printed:

JSON
{
    "name": "John",
    "age": 30,
    "city": "New York"
}

As you can see, the pretty-printed version is much easier to read and understand, with each key-value pair on its own line and indented for clarity. Many programming languages and text editors have built-in functions or extensions for pretty-printing data structures like JSON

How to pretty-print a JSON file in Python

To pretty-print a JSON file, you can use a text editor, an online tool, or a programming language that has a built-in JSON formatting function. Here are some ways to pretty-print a JSON file:

  1. Using a text editor: Most text editors such as Sublime Text, Visual Studio Code, and Notepad++ have a built-in JSON formatting function. You can select the entire JSON text and then use the formatting function to reformat it in a readable way.
  2. Using an online tool: There are several online tools available that allow you to format a JSON file by pasting it into the tool’s input field. Examples include JSON Formatter & Validator, JSON Editor Online, and JSON Viewer.
  3. Using a programming language: If you have access to a programming language such as Python, you can pretty-print a JSON file using the built-in json module. Here’s an example code snippet:
Python
import json

with open('my_file.json') as f:
    data = json.load(f)

print(json.dumps(data, indent=4))

In this example, the ‘json.load()‘ method reads the JSON data from the file into a Python dictionary. The ‘json.dumps()‘ method is then used to pretty-print the dictionary to the console with an indent of 4 spaces.

Pretty-print a JSON file in JavaScript

In JavaScript, you can pretty-print a JSON file by using the built-in ‘JSON.stringify()‘ method with the space parameter. Here is an example code snippet:

JavaScript
const data = {
  "name": "John Doe",
  "age": 30,
  "email": "[email protected]",
  "isMarried": true,
  "hobbies": ["reading", "traveling", "cooking"],
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": "12345"
  }
};

const prettyData = JSON.stringify(data, null, 2);

console.log(prettyData);

In this example, the ‘JSON.stringify()‘ method is used to convert the ‘data‘ object to a JSON string with an indent of 2 spaces. The resulting pretty-printed JSON string is stored in the ‘prettyData‘ variable, which is then logged to the console.

Note that the second parameter of ‘JSON.stringify()‘ is used to specify a “replacer” function, which can be used to filter and transform the output. In this example, we set it to ‘null‘ because we want to include all properties in the output. The third parameter is used to specify the number of spaces to use for indentation (in this case, 2).

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.