EasyπŸ‘€ 0-2 yearsπŸ‘€ 3-5 years 1 min read

Pretty Printing JSON β€” Interview Questions

Asked inInfosysTCSCognizantWipro
#json#pretty print#formatting#stringify#indent
Report issue

⚑ 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

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

⌨️Hands-on Keyboard

Learn by Doing

javascript
const obj = { id: 1, tags: ["a", "b"] };

console.log(JSON.stringify(obj));          // minified
console.log(JSON.stringify(obj, null, 2)); // pretty (2-space indent)
Output
{"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:

βœ“ 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

  • 1Why minify JSON for network responses?
  • 2What is the third argument of JSON.stringify?
  • 3How does gzip change the size argument?

🧩Related Technologies

JSON.stringifyjqgzipdevtools

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?

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.

Related Questions