ToolHubTools

JSON Formatter

Online JSON formatting, compression, validation and escaping with automatic error positioning.

Input JSON
Result
Sponsored

Features

  • Paste JSON text and click Format to auto-indent and beautify
  • Click Compress to minify JSON to a single line
  • Click Validate to check JSON syntax and show error position
  • One-click copy result and clear content

FAQ

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data interchange format based on JavaScript syntax but language-independent. It uses key-value pairs and array structures, making it easy for humans to read and write, and for machines to parse and generate. It has become the de facto standard for Web API data transmission.

What is the difference between JSON and XML?

JSON is more lightweight than XML, without the redundant overhead of closing tags, and parses faster. JSON natively supports arrays, while XML requires repeated tags to simulate them. JSON data structures are closer to modern programming language object models, but XML has advantages in document markup and mixed content.

What are common JSON syntax errors?

The most common ones are: a trailing comma after the last key-value pair, strings not using double quotes (single quotes are invalid), key names without quotes, and unsupported comments (the JSON standard does not allow comments). This tool validation feature automatically locates these errors.

How to handle Chinese characters in JSON?

Standard JSON supports UTF-8 encoded Chinese, which can be written directly. However, some APIs return JSON with Chinese characters escaped as \uXXXX format (e.g., \u4e2d\u6587). This tool automatically handles Unicode escaping during formatting, displaying readable Chinese.

How large a JSON file is supported?

Limited by browser memory, it can typically handle JSON text of several MB. For very large files (e.g., 10MB+), it is recommended to use dedicated streaming parsing tools (such as JSON Streaming) to avoid memory overflow.

Is data uploaded to the server?

No. All JSON processing is done locally in the browser, with no data uploaded, ensuring safety and reliability. You can use it with confidence even when it contains sensitive information.

What is the difference between JSON.stringify and JSON.parse?

JSON.stringify() serializes a JavaScript object into a JSON string, while JSON.parse() parses a JSON string into a JavaScript object. The formatter essentially parses first and then stringifies with specified indentation; compression is stringify without indentation.

How to format deeply nested JSON?

This tool automatically indents by hierarchy, adding 2 spaces of indentation per level. For extremely deep JSON (e.g., over 20 levels), readability will still be limited. It is recommended to first locate the target level via path, then format that part separately.

About JSON Formatter

Free online JSON formatter, compressor, validator and escape tool with error positioning. No installation required. This tool runs entirely in your browser. No data is uploaded to the server, helping protect your privacy. Everything works instantly with no software or plugin required.

The Complete JSON Formatting Guide: Syntax, Best Practices, and Common Issues

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data interchange format, standardized by Douglas Crockford in the early 2000s. It is based on JavaScript object literal syntax but, as a text format independent of programming languages, almost all modern languages provide native support.

JSON design goal is a balance between "human-readable" and "machine-parseable." It uses simple key-value pairs and array structures to represent data, without XML tag redundancy or YAML indentation sensitivity. Today, RESTful APIs, configuration files, NoSQL databases (such as MongoDB), and other scenarios almost all use JSON as the data carrier.

The Six Data Types of JSON

The JSON standard defines six data types: String (must be wrapped in double quotes), Number (supports integers and floating-point numbers, does not support NaN/Infinity), Boolean (true or false), null (note the lowercase), Object (unordered key-value pair collection), and Array (ordered value collection).

It is particularly important to note that JSON does not support the following JavaScript types: undefined, Function, Symbol, BigInt, and Date objects. These values are ignored or converted to null during JSON.stringify. If you need to pass dates in JSON, they are usually represented as ISO 8601 format strings.

The Purpose of JSON Formatting

JSON data in production environments is usually compressed (no indentation, no line breaks) to reduce transmission size. For example, a JSON returned by an API might be a single line of text, almost impossible to read manually. Formatting (also called beautifying/Prettify) re-adds indentation and line breaks to make the data structure clear at a glance.

Common indentation options for formatting are 2 spaces (most common) or 4 spaces (more spacious). Tab indentation, while valid in JSON, is less universal than spaces and can display inconsistently across different editors. This tool defaults to 2-space indentation, which is also the default setting for mainstream tools like Prettier and ESLint.

JSON Validation and Common Error Troubleshooting

One of the core features of a JSON formatting tool is validation. Common errors include: trailing commas (an extra comma after the last property), single-quote strings (JSON only allows double quotes), unquoted key names, comments (standard JSON does not support comments), and extra commas or brackets. A good formatting tool can precisely locate error positions.

Another common "pseudo-JSON" issue is the BOM header. Some Windows editors save files with a UTF-8 BOM (\uFEFF), which causes JSON.parse to throw "Unexpected token." The solution is to re-save the file in UTF-8 encoding without BOM. This tool automatically handles BOM headers.

JSON Security Considerations

JSON itself does not contain executable code, but parsing JSON with eval() is extremely dangerous (and has been deprecated) because attackers can inject malicious JavaScript. Modern browsers provide the safe JSON.parse(), which only parses pure data and does not execute any code. Never use eval() to process JSON from untrusted sources.

Another security risk is JSON Hijacking. If an API returns JSON data starting with a square bracket, attackers can read it cross-origin via a <script> tag. Defense methods include adding an infinite loop prefix (such as while(1);) before the JSON response, or using CORS headers to restrict origins. Modern browsers have largely eliminated this risk.

JSON in Real-World Development

RESTful APIs: Almost all modern Web APIs use JSON as the request and response format. Compared to XML, JSON is more lightweight, parses faster, and consumes less mobile data. Although GraphQL uses its own query language, its responses are also in JSON format.

Configuration files: package.json, tsconfig.json, .eslintrc.json, and other front-end toolchain configurations widely use JSON. Although JSON does not support comments (JSONC is an extended format), its strict syntax reduces configuration ambiguity. The NoSQL database MongoDB directly stores documents in BSON format (similar to JSON), and its query syntax is also JSON-style.

Sponsored