Pretty Printing JSON — Interview Questions
Reviewed by Gurusankar M.
Short Answer
Pretty-printing formats JSON with indentation and line breaks for human readability — JSON.stringify(obj, null, 2), json.dumps(obj, indent=2), or piping through jq. It's for logs, debugging, and config files; for network payloads you send minified (no spaces) JSON to save bytes.
On this page
Concept Made Simple
Coffee Chat Question
“What is pretty-printing JSON and how do you do it?”
Remember It Faster
Mind Map Answer
- JS
- JSON.stringify(obj, null, 2)
- Python
- json.dumps(obj, indent=2)
- CLI
- cat file.json | jq .
- Rule
- pretty for humans, minified on the wire
Learn by Doing
Hands-on Keyboard
const obj = { id: 1, tags: ["a", "b"] };
console.log(JSON.stringify(obj)); // minified
console.log(JSON.stringify(obj, null, 2)); // pretty (2-space indent){"id":1,"tags":["a","b"]}
{
"id": 1,
"tags": [
"a",
"b"
]
}Think Beyond the Expected
What If?
Should an API pretty-print its JSON responses?
By default, no — send minified JSON to reduce payload size and bandwidth (whitespace adds up at scale, and gzip handles the rest anyway). Offer pretty output only as an opt-in (e.g. ?pretty=1) for debugging. Pretty-printing is for humans reading logs/config, not for production traffic.
Real World
Developers pretty-print constantly while debugging — jq in the terminal, formatters in the browser devtools, indent=2 in scripts. Production endpoints stay minified so responses are as small as possible.
Interviewer's Expectation
Keywords they're listening for:
- indentation for readability
- stringify(obj,null,2) / dumps(indent=2)
- jq for CLI
- minify on the wire
- size vs readability trade-off
Common Mistakes
- Pretty-printing production API responses by default
- Assuming whitespace is free at scale
- Confusing minify with compression
Best Practices
- Minify responses; pretty-print only on opt-in
- Use indent for logs, config, and debugging
- Rely on gzip/brotli for transport size
Follow-up Questions
- Why minify JSON for network responses?
- What is the third argument of JSON.stringify?
- How does gzip change the size argument?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Serialization (JSON) Interview question: "What is pretty-printing JSON and how do you do it?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair