免费在线 JSON 格式化程序和验证程序
JSON 是 APIs 和配置文件的通用语言,但缩小或格式错误的 JSON 很难阅读和调试。 Dokall 的 JSON 格式化程序会验证您的输入,突出显示语法错误,并使用正确的缩进进行漂亮的打印。
您还可以缩小 JSON 以减少有效负载大小。一切都在客户端运行 - 您的 API 密钥和敏感数据永远不会接触我们的服务器。将此页面添加为书签,以便在开发过程中快速进行 JSON 调试。
What is JSON?
JSON (JavaScript Object Notation) is a lightweight text format for structured data. It was derived from JavaScript object syntax but is language-independent - virtually every programming language can parse and generate JSON. It has become the default data format for REST APIs, configuration files, and data storage.
JSON supports six data types: strings (double-quoted), numbers, booleans (true/false), null, arrays (ordered lists), and objects (key-value maps). Its simplicity is its strength - there are no attributes, namespaces, or schemas built into the format itself.
JSON Syntax Rules
JSON is strict about syntax. Keys must be double-quoted strings - single quotes and unquoted keys are not valid. Strings must use double quotes, not single quotes. Trailing commas after the last element in an array or object are not allowed.
Numbers cannot have leading zeros (except 0 itself) or a trailing decimal point. Booleans are lowercase true and false, not True/False or TRUE/FALSE. Comments are not part of the JSON specification - if your file has // or /* comments, it is not valid JSON (though some parsers support JSONC or JSON5 as extensions).
Whitespace between tokens is ignored, which is why you can minify JSON by removing all spaces and newlines, or pretty-print it with indentation for readability. Both represent the same data.
Common JSON Errors and How to Fix Them
"Unexpected token" usually means a comma, quote, or bracket is misplaced. The most common cause is a trailing comma - {"name": "test",} has a comma after the last property, which is valid in JavaScript but not in JSON.
"Unexpected end of input" means the parser reached the end of the string before all brackets or braces were closed. Count your opening and closing brackets. Editors with bracket matching help here.
"Bad control character" often means the string contains an unescaped newline, tab, or other control character. In JSON strings, newlines must be written as \n, tabs as \t, and backslashes as \\.
"Duplicate key" is technically allowed by the JSON specification, but most parsers use the last value and silently discard earlier ones. This can lead to confusing bugs. Dokall's formatter flags duplicate keys so you can catch them.
JSON vs XML vs YAML
JSON is compact and easy to parse programmatically. It has no comments, no attributes, and a simple type system. Best for: APIs, data interchange, configuration files where comments are not needed.
XML is verbose but supports attributes, namespaces, schemas (XSD), and transformation (XSLT). It is self-describing and can represent mixed content (text with inline markup). Best for: document-centric data, SOAP APIs, enterprise systems with strict schema requirements.
YAML is a superset of JSON with a cleaner syntax - it uses indentation instead of braces, supports comments, and has additional types like dates. However, its significant whitespace makes it error-prone to edit, and it has security concerns around arbitrary code execution in some parsers. Best for: configuration files (Kubernetes, Docker Compose, CI/CD), human-edited data.
For API responses and programmatic data exchange, JSON has essentially won. For config files that humans edit, YAML or TOML are often preferred.
Pretty-Print vs Minified JSON
Pretty-printed JSON uses newlines and indentation (typically 2 or 4 spaces) to make the structure visually clear. It is easier to read, review, and debug. Use it during development, in documentation, and when storing JSON in version-controlled config files where diffs matter.
Minified JSON strips all unnecessary whitespace. A 50 KB formatted JSON file might shrink to 30 KB minified. Over thousands of API responses, this reduces bandwidth, speeds up parsing, and lowers costs. Use minification for API responses in production, data stored in databases, and network payloads.
Dokall's formatter lets you switch between both with one click. The data is identical - only the whitespace changes. If you also need to sort keys for deterministic output (useful for comparing two JSON objects), the tool supports alphabetical key sorting.
Working with JSON in APIs
When sending JSON in an API request, set the Content-Type header to application/json. When receiving JSON, check that the response has this content type before parsing. Most HTTP clients handle this automatically, but mismatched content types are a frequent source of "unexpected token" errors.
For large JSON payloads, consider streaming parsers that process the data incrementally instead of loading everything into memory. In Node.js, libraries like JSONStream parse gigabyte-scale files. In Python, ijson serves the same purpose.
JSON does not natively support dates, binary data, or BigInt values. Dates are typically serialized as ISO 8601 strings (2024-01-15T10:30:00Z). Binary data is Base64-encoded. Large integers that exceed JavaScript's Number.MAX_SAFE_INTEGER should be sent as strings to prevent precision loss.