How to Pretty Print JSON in Different Programming Languages

JSONFormattingLanguagesDev Tips

Practical how-to for beautifying JSON in JavaScript, Python, Java, and more.

Introduction

When you’re dealing with JSON, whether it’s logs, API responses, or config files, reading a minified blob of text is a nightmare. Pretty printing JSON makes it human-readable by adding indentation and line breaks. This guide is a hands-on look at how to pretty print JSON in JavaScript, Python, Java, and beyond, with practical examples you can copy and use right away.


Why pretty print JSON?

Debugging: Easily spot errors or unexpected structures.

Logs: Clean JSON in logs makes it simpler to trace problems.

Code reviews: Makes data easy to follow for teammates.

Documentation: Beautified JSON is easier to include in wikis or tech docs.


Pretty printing JSON in JavaScript

JavaScript has JSON support built right in, and JSON.stringify is all you need.

Example: Pretty print an object

const data = {
  name: "Alice",
  age: 30,
  skills: ["JavaScript", "Node.js", "React"]
};

const pretty = JSON.stringify(data, null, 2);
console.log(pretty);
  • The null is for the replacer (not modifying values).
  • 2 means indent by two spaces. Use 4 if you like more indentation.

From JSON string to pretty string

If you already have a JSON string:

const jsonString = '{"name":"Alice","age":30}';
const obj = JSON.parse(jsonString);
console.log(JSON.stringify(obj, null, 2));

✅ Tip: Many browser consoles and Node.js automatically pretty print JSON objects you log directly.


Pretty printing JSON in Python

Python’s json module makes this super straightforward.

Example: Pretty print a dictionary

import json

data = {
    "name": "Alice",
    "age": 30,
    "skills": ["Python", "Django", "Flask"]
}

print(json.dumps(data, indent=4))
  • indent=4 sets the indentation level.

From JSON string

json_string = '{"name":"Alice","age":30}'
data = json.loads(json_string)
print(json.dumps(data, indent=2))

✅ Tip: The json.tool CLI module is also handy:

cat data.json | python -m json.tool

This reads your file and prints it in a clean, indented format.


Pretty printing JSON in Java

Java doesn’t have built-in JSON, so you’ll usually use a library like Jackson or Gson.

With Jackson

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

public class PrettyJson {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Object data = mapper.readValue("{\"name\":\"Alice\",\"age\":30}", Object.class);

        ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
        String prettyJson = writer.writeValueAsString(data);

        System.out.println(prettyJson);
    }
}

With Gson

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class PrettyJsonGson {
    public static void main(String[] args) {
        String json = "{\"name\":\"Alice\",\"age\":30}";
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        Object obj = gson.fromJson(json, Object.class);
        String prettyJson = gson.toJson(obj);

        System.out.println(prettyJson);
    }
}

✅ Tip: Gson is lighter, Jackson is more powerful for complex scenarios.


Pretty printing JSON in other languages

Bash / jq

If you’re in a Unix shell, jq is the king of JSON formatting:

cat data.json | jq .

Or format inline JSON:

echo '{"name":"Alice","age":30}' | jq .

✅ You can also colorize output, slice, and filter JSON easily.

PHP

<?php
$data = ["name" => "Alice", "age" => 30];
$json = json_encode($data, JSON_PRETTY_PRINT);
echo $json;
?>

Ruby

require 'json'
data = {"name" => "Alice", "age" => 30}
puts JSON.pretty_generate(data)

When to pretty print vs. minify

Pretty print:

  • Debugging
  • Logs
  • Developer tools

Minify:

  • Sending over networks
  • Storing in production databases

Most APIs send minified JSON for efficiency, but developers almost always pretty print during debugging.


Additional tips

  • Many editors (VS Code, Sublime, IntelliJ) have built-in JSON formatters.
  • Online tools like jsonformatter.org or json.toolaska.com let you paste and beautify quickly.
  • Automate formatting in scripts so your logs are always clean.

Wrapping up

Pretty printing JSON isn’t just about aesthetics — it’s a crucial tool for understanding and debugging your data. Whether you’re working in JavaScript, Python, Java, or using tools like jq in Bash, taking a minute to format your JSON pays off in saved debugging hours and clearer communication with your team.

Happy coding and happy debugging! 🚀