JSON Formatter
Fast, free, and secure online JSON formatter.
Format your JSON below
Quickly format your JSON online with instant results. Enjoy a modern, code-friendly editor featuring line numbers and syntax highlighting. All operations are fast, private, and never leave your browser.
What is JSON Formatting?
JSON formatting is the process of transforming JSON data into a consistent, human-readable representation by adding indentation, whitespace, and line breaks—without altering its structure or content. Proper formatting makes JSON easier to inspect, debug, share, and edit, while still ensuring compatibility with machines and software tools.
JSON, by definition, ignores whitespace outside of string values. However, poorly formatted (or minified) JSON—lacking indentation or collapsed onto a single line—can be nearly impossible for humans to read or modify reliably.
Formatting vs. Minifying JSON
- Formatting adds whitespace, indentation, and line breaks for clarity and readability.
- Minifying removes all unnecessary whitespace for maximum compactness and efficiency in storage or transfer.
- A robust JSON formatter lets you switch between these modes, making it easy to toggle between human-friendly and machine-optimized versions as needed.
Example: Minified vs. Formatted JSON
Minified (compact) JSON:{"id":"3f4b2c","user":{"name":"Dana","is_active":true},"roles":["admin","editor"],"count":7}Formatted (pretty-printed) JSON:
{ "id": "3f4b2c", "user": { "name": "Dana", "is_active": true }, "roles": [ "admin", "editor" ], "count": 7 }
Why Format JSON?
- Readability: Proper indentation and line breaks make it easier to inspect nested objects, spot mistakes, and understand complex data structures at a glance.
- Debugging: Troubleshooting invalid or unexpected data is much simpler when you can visually scan and trace keys, values, and nesting levels.
- Collaboration: Well-formatted JSON is easier to review, discuss, and edit in code reviews, documentation, or shared files.
- Version Control: Git and other VCS tools produce more meaningful diffs with formatted JSON, making it easier to track changes over time.
- Compliance: Many style guides and automated linters (such as Prettier, ESLint, or jq) enforce consistent formatting for clarity and standardization.
- Tool Compatibility: Some APIs, CLIs, and editors expect formatted input for easier human interaction or logging.
How Does a JSON Formatter Work?
- Parsing: The formatter first attempts to parse your input text using a strict JSON parser. This step checks for syntax validity—catching issues such as missing quotes, trailing commas, or unescaped characters.
- Pretty-Printing: Once valid, the parsed data is serialized back to a string with user-defined indentation (commonly 2 or 4 spaces) and line breaks, creating a "pretty-printed" version.
If the input is not valid JSON, the formatter will throw an error or provide a helpful message indicating the location and nature of the problem.
Formatting Options
- Indentation: Set the number of spaces per level (up to 8).
- Sort object keys alphabetically
- Use tabs for indentation instead of spaces
- Escape non-ASCII characters as Unicode
- Minify output (single-line, no spaces)
Common Issues Solved by Formatting
- Single-line/Minified JSON: Data returned from APIs or log files is often minified for bandwidth efficiency, making manual editing difficult. Formatting restores readability for review and editing.
- Inconsistent Indentation: JSON pasted from various sources may have mixed tabs, spaces, or inconsistent indentation depths. Reformatting normalizes these differences, improving clarity and consistency.
- Large/Nested Structures: Deeply nested arrays or objects (such as configuration files or complex API responses) become tractable and easy to navigate when formatted, with collapsible views in supported editors.
Real-World Use Case: Formatting API Responses
When integrating with third-party APIs (like AWS, Stripe, or Google Cloud), responses are often compacted for speed. Formatting the JSON output makes it easier to inspect for missing fields, debug unexpected values, or share with teammates.
Example: Raw API Response{"amount":2500,"currency":"usd","status":"succeeded","charges":[{"id":"ch_1Gq","amount":2500}]}Formatted for Review
{ "amount": 2500, "currency": "usd", "status": "succeeded", "charges": [ { "id": "ch_1Gq", "amount": 2500 } ] }
How to Format JSON with This Tool
- Paste or upload your raw, minified, or poorly-formatted JSON into the input area.
- Select formatting options (indentation size, sort keys, etc.).
- Click "Format" to process your input.
- View or copy the clean, readable output. If errors are found, detailed syntax messages will appear to help you fix the JSON.
All formatting is performed securely in your browser—your data never leaves your device.
Code Examples for JSON Formatting
See how to format JSON in various programming languages. These examples demonstrate basic formatting techniques.
const ugly = '{"name":"Alice","age":30,"roles":["admin","user"]}';
const pretty = JSON.stringify(JSON.parse(ugly), null, 2);
console.log(pretty);
import json
ugly = '{"name":"Alice","age":30,"roles":["admin","user"]}'
pretty = json.dumps(json.loads(ugly), indent=2)
print(pretty)
package main
import (
"encoding/json"
"fmt"
)
func main() {
ugly := []byte(`{"name":"Alice","age":30,"roles":["admin","user"]}`)
var obj interface{}
json.Unmarshal(ugly, &obj)
pretty, _ := json.MarshalIndent(obj, "", " ")
fmt.Println(string(pretty))
}
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
String ugly = "{"name":"Alice","age":30,"roles":["admin","user"]}";
ObjectMapper mapper = new ObjectMapper();
Object obj = mapper.readValue(ugly, Object.class);
String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
System.out.println(pretty);
}
}
using System;
using Newtonsoft.Json;
class Program {
static void Main() {
var ugly = "{"name":"Alice","age":30,"roles":["admin","user"]}";
var parsed = JsonConvert.DeserializeObject(ugly);
var pretty = JsonConvert.SerializeObject(parsed, Formatting.Indented);
Console.WriteLine(pretty);
}
}
<?php
$ugly = '{"name":"Alice","age":30,"roles":["admin","user"]}';
$obj = json_decode($ugly);
echo json_encode($obj, JSON_PRETTY_PRINT);
require 'json'
ugly = '{"name":"Alice","age":30,"roles":["admin","user"]}'
pretty = JSON.pretty_generate(JSON.parse(ugly))
puts pretty
echo '{"name":"Alice","age":30,"roles":["admin","user"]}' | jq .
fn main() {
let ugly = r#"{"name":"Alice","age":30,"roles":["admin","user"]}"#;
let value: serde_json::Value = serde_json::from_str(ugly).unwrap();
let pretty = serde_json::to_string_pretty(&value).unwrap();
println!("{}", pretty);
}
import com.fasterxml.jackson.databind.ObjectMapper
fun main() {
val ugly = "{"name":"Alice","age":30,"roles":["admin","user"]}"
val mapper = ObjectMapper()
val obj = mapper.readValue(ugly, Any::class.java)
val pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj)
println(pretty)
}
import Foundation
let ugly = "{\"name\":\"Alice\",\"age\":30,\"roles\":[\"admin\",\"user\"]}"
if let data = ugly.data(using: .utf8),
let obj = try? JSONSerialization.jsonObject(with: data),
let pretty = try? JSONSerialization.data(withJSONObject: obj, options: .prettyPrinted),
let prettyString = String(data: pretty, encoding: .utf8) {
print(prettyString)
}
const ugly = '{"name":"Alice","age":30,"roles":["admin","user"]}';
const pretty = JSON.stringify(JSON.parse(ugly), null, 2);
console.log(pretty);
SELECT jsonb_pretty('{"name":"Alice","age":30,"roles":["admin","user"]}'::jsonb);
SELECT JSON_PRETTY('{"name":"Alice","age":30,"roles":["admin","user"]}');
$ugly = '{"name":"Alice","age":30,"roles":["admin","user"]}'
$obj = $ugly | ConvertFrom-Json
$pretty = $obj | ConvertTo-Json -Depth 10
Write-Output $pretty
use JSON;
my $ugly = '{"name":"Alice","age":30,"roles":["admin","user"]}';
my $obj = decode_json($ugly);
print to_json($obj, { pretty => 1 });
import 'dart:convert';
void main() {
const ugly = '{"name":"Alice","age":30,"roles":["admin","user"]}';
final obj = jsonDecode(ugly);
final pretty = JsonEncoder.withIndent(' ').convert(obj);
print(pretty);
}
ugly = ~s({"name":"Alice","age":30,"roles":["admin","user"]})
{:ok, obj} = Jason.decode(ugly)
pretty = Jason.encode!(obj, pretty: true)
IO.puts(pretty)
import play.api.libs.json._
object Main extends App {
val ugly = """{"name":"Alice","age":30,"roles":["admin","user"]}"""
val jsValue = Json.parse(ugly)
val pretty = Json.prettyPrint(jsValue)
println(pretty)
}