Pretty Printing JSON β Interview Questions
β‘ 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.
βCoffee Chat Question
Concept Made Simple
βWhat is pretty-printing JSON and how do you do it?β
π§ Mind Map Answer
Remember It Faster
β¨οΈHands-on Keyboard
Learn by Doing
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"
]
}π₯What If?
Think Beyond the Expected
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:
β οΈ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
- 1Why minify JSON for network responses?
- 2What is the third argument of JSON.stringify?
- 3How does gzip change the size argument?
π§©Related Technologies
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?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.