jsonvscodeformattingjqdeveloper tools
How to Format JSON in VS Code, Terminal, and Online
Multiple ways to format JSON - VS Code built-in, command line with jq, Python, Node.js, and browser-based tools.
June 5, 2024ยท5 min read
Format JSON in VS Code
The easiest way - no plugins needed:
- Open your JSON file
- Press
Shift+Alt+F(Windows/Linux) orShift+Option+F(Mac) - Or right-click โ Format Document
For JSON in a non-.json file, use Ctrl+Shift+P โ "Format Document With..." โ JSON Language Features.
Format JSON in the Terminal
Using jq (recommended)
# Install: brew install jq (mac) or apt install jq (linux)
cat data.json | jq '.'
# From a string
echo '{"name":"Alice","age":30}' | jq '.'
Using Python (built-in)
echo '{"name":"Alice"}' | python3 -m json.tool
# Or from a file
python3 -m json.tool data.json
Using Node.js
node -e "process.stdin||(d='');process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>console.log(JSON.stringify(JSON.parse(d),null,2)))" < data.json
Format JSON in JavaScript
const ugly = '{"name":"Alice","age":30}'
const pretty = JSON.stringify(JSON.parse(ugly), null, 2)
console.log(pretty)
Online - Fastest Option
For quick one-off formatting without opening a terminal or IDE, use our JSON Formatter - paste and format instantly with syntax highlighting, error detection, and tree view.