← Back to Blog

How to Pretty Print JSON in Every Major Language

March 15, 2026 3 min read By CodeTidy Team

The JSON Pretty Print Predicament

Have you ever found yourself staring at a wall of minified JSON, wishing there was a way to make it more readable? You're not alone. We've all been there, trying to decipher a mess of curly braces and commas. But fear not, dear developer, for we're about to embark on a journey to make your JSON pretty printing dreams come true.

Table of Contents

  • Why Pretty Print JSON Matters
  • Pretty Printing JSON in Popular Languages
  • Advanced Pretty Printing Techniques
  • Handling Errors and Edge Cases
  • Key Takeaways
  • FAQ

Why Pretty Print JSON Matters

JSON (JavaScript Object Notation) is a lightweight data interchange format that's widely used in web development. However, its compact nature can make it difficult to read and understand, especially for complex data structures. Pretty printing JSON can greatly improve its readability, making it easier to debug, test, and maintain your code.

Pretty Printing JSON in Popular Languages

Let's dive into the world of pretty printing JSON in various programming languages. We'll cover the most popular ones, from Python to Ruby.

Python

In Python, you can use the json module to pretty print JSON. Here's an example:

import json

data = {'name': 'John', 'age': 30, 'city': 'New York'}
print(json.dumps(data, indent=4))

This will output:

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

We recommend using the indent parameter to specify the number of spaces for indentation.

JavaScript

In JavaScript, you can use the JSON.stringify() method to pretty print JSON. Here's an example:

const data = { name: 'John', age: 30, city: 'New York' };
console.log(JSON.stringify(data, null, 4));

This will output:

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

Note that the third argument to JSON.stringify() specifies the number of spaces for indentation.

Go

In Go, you can use the encoding/json package to pretty print JSON. Here's an example:

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    data := struct {
        Name string
        Age  int
        City string
    }{
        Name: "John",
        Age:  30,
        City: "New York",
    }
    jsonBytes, err := json.MarshalIndent(data, "", "    ")
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(string(jsonBytes))
    }
}

This will output:

{
    "Name": "John",
    "Age": 30,
    "City": "New York"
}

We recommend using the MarshalIndent() function to pretty print JSON.

Rust

In Rust, you can use the serde_json crate to pretty print JSON. Here's an example:

use serde_json::json;

fn main() {
    let data = json!({
        "name": "John",
        "age": 30,
        "city": "New York"
    });
    println!("{}", serde_json::to_string_pretty(&data).unwrap());
}

This will output:

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

Note that you'll need to add serde_json to your Cargo.toml file.

Java

In Java, you can use the Jackson library to pretty print JSON. Here's an example:

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        Object obj = mapper.readValue(json, Object.class);
        String prettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        System.out.println(prettyJson);
    }
}

This will output:

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

We recommend using the writerWithDefaultPrettyPrinter() method to pretty print JSON.

C#

In C#, you can use the Newtonsoft.Json library to pretty print JSON. Here's an example:

using Newtonsoft.Json;

public class Main {
    public static void Main() {
        string json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        JObject obj = JObject.Parse(json);
        string prettyJson = obj.ToString(Formatting.Indented);
        Console.WriteLine(prettyJson);
    }
}

This will output:

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

Note that you'll need to install the Newtonsoft.Json NuGet package.

PHP

In PHP, you can use the json_encode() function to pretty print JSON. Here's an example:

$data = array('name' => 'John', 'age' => 30, 'city' => 'New York');
$prettyJson = json_encode($data, JSON_PRETTY_PRINT);
echo $prettyJson;

This will output:

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

We recommend using the JSON_PRETTY_PRINT constant to pretty print JSON.

Ruby

In Ruby, you can use the json gem to pretty print JSON. Here's an example:

require 'json'

data = { name: 'John', age: 30, city: 'New York' }
pretty_json = JSON.pretty_generate(data)
puts pretty_json

This will output:

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

Note that you'll need to add json to your Gemfile.

Advanced Pretty Printing Techniques

In addition to the basic pretty printing techniques shown above, there are some advanced techniques you can use to customize the output.

  • Indentation: Most libraries allow you to specify the number of spaces for indentation.
  • Line wrapping: Some libraries allow you to specify the maximum line length.
  • Sorting: Some libraries allow you to sort the keys in the JSON object.

Handling Errors and Edge Cases

When pretty printing JSON, there are some errors and edge cases to consider.

  • Invalid JSON: Make sure to handle invalid JSON input gracefully.
  • Large JSON objects: Be mindful of performance when pretty printing large JSON objects.
  • Circular references: Some libraries may not handle circular references correctly.

Key Takeaways

  • Pretty printing JSON can greatly improve its readability.
  • Most programming languages have libraries or built-in functions for pretty printing JSON.
  • Be mindful of indentation, line wrapping, and sorting when customizing the output.
  • Handle errors and edge cases, such as invalid JSON and circular references.

FAQ

Q: Why is pretty printing JSON important?

Pretty printing JSON can greatly improve its readability, making it easier to debug, test, and maintain your code.

Q: What is the best way to pretty print JSON in Python?

We recommend using the json module with the indent parameter.

Q: Can I pretty print JSON in JavaScript without using a library?

Yes, you can use the JSON.stringify() method with the third argument specifying the number of spaces for indentation.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp