DEV Community

Harun Mahmud
Harun Mahmud

Posted on

JSON Formatter: 5 Ways to Pretty-Print JSON (Online, CLI, VS Code, JavaScript & Python)

If you've ever stared at a wall of minified JSON like this:

{"user":{"id":1,"name":"John","email":"john@example.com","roles":["admin","editor"],"active":true}}
Enter fullscreen mode Exit fullscreen mode

You know how painful it is to read. Here are 5 ways to format it instantly depending on where you're working.


1. Online JSON Formatter (fastest, no install)

If you just need to quickly format a JSON response from an API or debug a config file, an online tool is the fastest option.

The important thing is to use one that runs in your browser and doesn't send your data to a server — especially if your JSON contains tokens, user data, or anything sensitive.

👉 ToolsCraft JSON Formatter — formats, validates, and minifies JSON entirely client-side. Nothing leaves your machine.


2. VS Code (best for files)

VS Code has JSON formatting built in. Open your .json file and press:

  • Windows/Linux: Shift + Alt + F
  • Mac: Shift + Option + F

Or right-click → Format Document.

To auto-format on save, add this to your settings.json:

{
  "[json]": {
    "editor.formatOnSave": true
  }
}
Enter fullscreen mode Exit fullscreen mode

3. JavaScript (in your code)

const ugly = '{"name":"John","age":30,"city":"New York"}';
const pretty = JSON.stringify(JSON.parse(ugly), null, 2);
console.log(pretty);
Enter fullscreen mode Exit fullscreen mode

The third argument in JSON.stringify is the indentation level. Use 2 or 4 depending on your preference.


4. Python (scripts & data processing)

import json

ugly = '{"name":"John","age":30,"city":"New York"}'
parsed = json.loads(ugly)
pretty = json.dumps(parsed, indent=2)
print(pretty)
Enter fullscreen mode Exit fullscreen mode

Or directly from the terminal on any JSON file:

python -m json.tool yourfile.json
Enter fullscreen mode Exit fullscreen mode

5. Command Line with jq (power users)

# Install
brew install jq  # Mac
apt-get install jq  # Linux

# Format a file
jq . yourfile.json

# Format API response directly
curl https://api.example.com/users | jq .
Enter fullscreen mode Exit fullscreen mode

Quick comparison

Method Best for Install needed
Online tool Quick debugging, API responses No
VS Code Working with JSON files No
JavaScript In your app code No
Python Scripts, data processing No
jq Terminal, pipelines Yes

Bonus: Validate while formatting

A good formatter should also validate your JSON and tell you exactly where the error is. If you paste invalid JSON:

{"name": "John", "age": 30,}
Enter fullscreen mode Exit fullscreen mode

That trailing comma makes it invalid. A proper formatter should highlight the exact line and character where the error is — not just silently fail.

The ToolsCraft JSON Formatter shows inline error highlighting, which saves a lot of head-scratching.


That's it! Use the online tool for quick jobs, VS Code for files, and jq if you live in the terminal.

What's your go-to method for formatting JSON? Drop it in the comments 👇

Top comments (0)