JSON Pretty Print
Pretty print here, or copy the one-liner for Python, JavaScript, jq and six more.
Nothing you paste leaves your browser. The connect-src allowlist makes that a browser guarantee rather than a promise. Check it yourself
Pretty printing and formatting are the same operation under two names. Paste a document above to do it here, or take the one-liner for whichever language you are already in.
The examples below are the versions that actually work, including the arguments people usually discover only after their output comes out wrong.
The arguments that catch people out
- Python's ensure_ascii
- Defaults to True, which escapes every non-ASCII character. A document full of names or emoji comes back as a wall of \u sequences. Pass ensure_ascii=False.
- Python's separators when minifying
- json.dumps leaves a space after every comma and colon unless you pass separators=(",", ":"), so the naive minify call is not minified.
- PHP's slash escaping
- json_encode escapes forward slashes and Unicode by default. JSON_UNESCAPED_SLASHES and JSON_UNESCAPED_UNICODE turn that off. PHP also indents with four spaces and does not let you change it.
- Go's html escaping
- The encoder escapes <, > and & to \u003c and friends by default, for safe embedding in HTML. SetEscapeHTML(false) turns it off.
- Every one of these round-trips your numbers
- Parsing and re-serialising rewrites number text. 1.50 becomes 1.5 and a 19-digit ID loses its last digits. Go is the exception: json.Indent works on raw bytes.
How to do this in code
Pretty printing in nine languages and tools.
js JavaScript
JSON.stringify(JSON.parse(text), null, 2) py Python
json.dumps(json.loads(text), indent=2, ensure_ascii=False)
# From a file, on the command line
python -m json.tool --indent 2 --no-ensure-ascii in.json out.json sh jq and the shell
jq . file.json
curl -s https://api.example.com/x | jq .
pbpaste | jq . # macOS, from the clipboard
jq -S . file.json # sorted keys, for stable diffs go Go
json.Indent reformats the bytes without decoding, so your numbers are untouched.
var out bytes.Buffer
json.Indent(&out, data, "", " ")
fmt.Println(out.String()) rb Ruby
JSON.pretty_generate(JSON.parse(text)) php PHP
json_encode(json_decode($text), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) java Java
ObjectMapper mapper = new ObjectMapper();
Object value = mapper.readValue(text, Object.class);
String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value); rs Rust
Enable serde_json’s arbitrary_precision feature to keep number text intact through the round trip.
let value: serde_json::Value = serde_json::from_str(text)?;
let pretty = serde_json::to_string_pretty(&value)?; cs C#
using var doc = JsonDocument.Parse(text);
var pretty = JsonSerializer.Serialize(doc, new JsonSerializerOptions { WriteIndented = true }); Questions
- Is pretty print different from beautify or format?
- No. All three mean adding whitespace so a human can read the document. Different communities settled on different words for it.
- How do I pretty print JSON in VS Code?
- Open the file and press Shift+Alt+F on Windows and Linux, or Shift+Option+F on macOS. For a snippet you have pasted into an untitled file, set the language mode to JSON first or the formatter will not know what to do with it.
- How do I pretty print a curl response?
- Pipe it: curl -s URL | jq . if you have jq, or curl -s URL | python -m json.tool if you do not. Add -s to curl or its progress meter ends up in the pipe.