JavaScript Pretty Print: A Practical Guide to Beautifying JSON, Objects, and Code

A thorough guide to javascript pretty print techniques, covering JSON.stringify usage, Node.js utilities, editor integrations, and CLI tricks to produce readable data structures and well-formatted code in 2026.

JavaScripting
JavaScripting Team
·5 min read
JS Pretty Print - JavaScripting
Photo by kaboompicsvia Pixabay
Quick AnswerDefinition

According to JavaScripting, JavaScript pretty print typically means formatting data and code for readability. For data, the standard approach is JSON.stringify with a space or tab indent; for code, use a formatter integrated in editors or tools like Prettier. This quick guide covers practical JSON pretty print, object formatting, and basic command-line helpers.

What does 'javascript pretty print' mean?

In practice, javascript pretty print refers to making data structures and source code easy to read at a glance. For data payloads, JSON.stringify(obj, replacer, space) is the go-to method to insert indentation. For source code, editors and formatters normalize style automatically. According to JavaScripting, the simplest entry point is JSON.stringify(obj, null, 2) for JSON data. The JavaScripting team notes that 2026 tooling increasingly relies on consistent indentation to reduce cognitive load.

JavaScript
const data = { a: 1, b: { c: 2 } }; console.log(JSON.stringify(data, null, 2));

This prints a human-friendly JSON block with two-space indentation. For a deeper look, consider Node.js utilities like util.inspect, which can present objects with color and depth.

JavaScript
const util = require('util'); const data = { a: 1, b: { c: 2 } }; console.log(util.inspect(data, { depth: null, colors: true }));

You can also run a quick in-line print from the shell:

Bash
node -e 'console.log(JSON.stringify({a:1,b:[2,3]}, null, 2))'

output

Steps

Estimated time: 60-90 minutes

  1. 1

    Identify the target for pretty print

    Decide whether you are formatting data (JSON) or source code, then pick the right tool: JSON.stringify for data and a formatter for code. Consider project size and readability requirements.

    Tip: Match indentation to team conventions.
  2. 2

    Format JSON data in code

    Create a small helper or use a one-liner to print a JSON object with indentation to console or a log file.

    Tip: Keep replacer null unless you need to filter keys.
  3. 3

    Leverage editor tooling

    Install and configure a formatter (e.g., Prettier) and set it to format on save. Add a minimal .prettierrc.json to enforce style.

    Tip: Align tabWidth across the project.
  4. 4

    Explore Node.js utilities

    Use util.inspect for deeper, customizable in-memory views of objects during debugging.

    Tip: Depth null shows full structure.
  5. 5

    Try CLI formatting</title>

    Format JSON from the command line using jq or Python's json.tool for quick previews in pipelines.

    Tip: Pipelines benefit from color output.
  6. 6

    Verify readability impact

    Review pretty-printed output with peers to ensure readability gains do not impede scanning speed.

    Tip: Consider audience of logs.
Pro Tip: Enable a project-wide formatter to standardize output across files.
Warning: Be cautious with deep pretty printing of very large objects; it can degrade performance during logging.
Note: Use a replacer with JSON.stringify to omit sensitive fields during debugging.

Prerequisites

Required

Keyboard Shortcuts

ActionShortcut
CopyCopy code or resultsCtrl+C
PasteInsert from clipboardCtrl+V
Format DocumentVS Code or any editor with Prettier+Alt+F
FindSearch within the documentCtrl+F

Questions & Answers

What is the simplest way to pretty print JSON in JavaScript?

The simplest approach is JSON.stringify(obj, null, 2). This prints the object with two-space indentation for readability. For quick console checks, you can wrap it in a small helper function.

You can pretty print JSON in one line with JSON.stringify(obj, null, 2).

Can I pretty print code automatically in editors?

Yes. Install a formatter like Prettier, enable format-on-save, and configure a shared style guide. This ensures consistent indentation and line breaks across your project.

Yes, use a code formatter like Prettier to auto-format on save.

What tools help with CLI pretty printing?

jq is a powerful JSON processor for pretty printing and querying JSON from the command line. Python's json.tool is another simple built-in option.

Try jq or Python's json.tool for command-line pretty printing.

Is pretty printing always better for logs?

Pretty printing improves readability but can increase log size. For production logs, consider truncating or redacting sensitive fields while keeping enough structure for debugging.

Pretty printing is helpful, but balance readability with log size and privacy.

How deep should object prints go in Node.js?

Set depth to a reasonable level to avoid overwhelming console output. Use depth: null for full depth during deep debugging when needed.

Start with a shallow depth, increase as necessary for debugging.

What to Remember

  • Understand the distinction between data pretty printing and code formatting
  • Use JSON.stringify(obj, null, 2) for readable JSON outputs
  • Leverage editor formatters to maintain consistent style
  • Utilize Node.js util.inspect for rich object views during debugging
  • CLI tools like jq or json.tool help in pipelines and quick previews

Related Articles