Formatador JSON

Formate e embeleze dados JSON com destaque de sintaxe

What is JSON Formatting?

JSON formatting (also called pretty-printing or beautification) transforms compact, minified JSON into a human-readable representation with consistent indentation, line breaks, and spacing. This is essential for code reviews, debugging API responses, writing configuration files, and understanding complex nested data structures. The formatting process does not alter the data — it only changes whitespace, which JSON parsers ignore. The result is semantically identical to the input.

How JSON Formatting Works

A JSON formatter parses the input string into an abstract syntax tree (or iterates through tokens), then serializes it back to text with configurable indentation. The standard approach uses JSON.parse() followed by JSON.stringify(value, null, indent) where indent is typically 2 or 4 spaces. This two-step process also validates the JSON since JSON.parse() throws on invalid syntax.

Beyond basic indentation, advanced formatters handle edge cases: preserving key order (JSON objects are technically unordered, but developers expect stable output), handling extremely large numbers that exceed JavaScript's Number.MAX_SAFE_INTEGER, and formatting inline vs multi-line arrays based on content length. This tool uses 2-space indentation by default, which is the most common convention in web development and matches popular tools like Prettier and ESLint.

JSON Minification: The Inverse Operation

Minification removes all unnecessary whitespace (spaces, tabs, newlines) from JSON to produce the smallest possible byte representation. This is critical for production APIs where every byte affects response time and bandwidth costs. A typical API response might shrink 30–50% after minification, especially for deeply nested objects with long key names.

Use formatting during development and debugging; use minification for production payloads, database storage, and network transmission. This tool provides both operations so you can switch between human-readable and machine-optimized representations instantly.

JSON Validation and Error Detection

The formatter also acts as a validator: if the input contains syntax errors, it reports the error position with line and column numbers. Common JSON mistakes include: trailing commas after the last array element or object property (valid in JavaScript but illegal in JSON), single-quoted strings (JSON requires double quotes), unquoted keys, comments (JSON has no comment syntax), and undefined or NaN values (not valid JSON types).

When you encounter a "Unexpected token" error, the position indicator helps you locate the exact character that violates the grammar. This tool highlights the error location and suggests common fixes — for example, "Did you mean to use double quotes instead of single quotes?"

Working with Large JSON Documents

For documents larger than a few hundred kilobytes, formatting can take noticeable time. This tool processes JSON entirely in the browser using the V8 engine's native JSON parser, which handles multi-megabyte files efficiently. For extremely large files (10MB+), consider using streaming JSON parsers or command-line tools like jq which process data without loading everything into memory.

The Ace editor used in this tool provides syntax highlighting, bracket matching, code folding, and line numbers — making it practical to navigate and inspect large JSON structures directly in the browser without installing any software.

Code Examples

Formatting JSON with JavaScript's built-in API

const raw = '{"name":"John","age":30,"address":{"city":"NYC","zip":"10001"}}';
const formatted = JSON.stringify(JSON.parse(raw), null, 2);
console.log(formatted);

Output:

{
  "name": "John",
  "age": 30,
  "address": {
    "city": "NYC",
    "zip": "10001"
  }
}

Minifying JSON for API responses

// Remove all whitespace — produce smallest output
const obj = { users: [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }] };
const minified = JSON.stringify(obj);
console.log(minified.length); // Much smaller than formatted version

Output:

{"users":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}

Standards & Specifications

  • RFC 8259 (JSON) — The current JSON specification defining syntax, data types, and grammar rules
  • ECMA-404 — The ECMA standard for JSON Data Interchange Syntax (equivalent to RFC 8259)

Perguntas Frequentes

O que faz um formatador JSON?

Um formatador JSON pega JSON compacto e adiciona indentação e quebras de linha para torná-lo legível. Também valida a sintaxe.

A formatação altera meus dados?

Não. A formatação só adiciona espaços em branco que parsers ignoram. Seus dados permanecem iguais.

Quais são os erros JSON mais comuns?

Vírgulas finais, aspas simples, chaves sem aspas, valores undefined e chaves/colchetes não fechados.

Meus dados são enviados?

Não. Toda formatação acontece no navegador. Seus dados nunca saem do dispositivo.