# JSON formatter and validator

> Format (pretty-print) or minify JSON and validate it against RFC 8259, with the line and column of any syntax error explained in plain words.

Phiên bản tương tác: https://www.calcopenly.com/vi/programming/json-formatter-validator
Chủ đề: Máy tính lập trình và công nghệ

The validator parses the text with the RFC 8259 grammar: objects, arrays, double-quoted strings, numbers, true, false and null, and nothing else. If parsing succeeds, it prints the same values back, either with one member per line and 2 spaces, 4 spaces or a tab per level, or minified with every space outside strings removed. If it fails, it reports the line and column where the grammar broke and what it expected there.

Developers use it to read API responses and config files and to shrink payloads. The default object has 6 keys and 10 values nested 2 levels deep, and pretty-printing it with 2 spaces grows it from 125 to 168 bytes.

Strings and numbers are copied exactly as written, so escapes survive and a large integer such as 12345678901234567890123 keeps every digit.

## Dữ liệu đầu vào

- **JSON**
- **Đầu ra** (lựa chọn: Pretty-print, Minify)
- **Indent with** (lựa chọn: 2 spaces, 4 spaces, Tabs)
- **Sort keys**

## Kết quả

- Check — kết quả chính
- Formatted JSON
- Keys
- Nesting depth
- Values
- Input size (bytes)
- Output size (bytes)

## Ví dụ có lời giải

### Pretty-print with 2 spaces

- JSON: {"name":"CalcOpenly","tags":["math","finance"],"ok":true,"none":null}
- Đầu ra: Pretty-print
- Indent with: 2 spaces
- **Check: Valid JSON**
- **Formatted JSON: {
  "name": "CalcOpenly",
  "tags": [
    "math",
    "finance"
  ],
  "ok": true,
  "none": null
}**
- **Keys: 4**
- **Nesting depth: 2**
- **Values: 7**
- Nguồn đối chiếu: Python 3.8: json.dumps(json.loads(s), indent=2, ensure_ascii=False); value count by walking json.loads(s)

### Minify

- JSON: { / "a": [1, 2, {"b": "x y"}], / "c": false / }
- Đầu ra: Minify
- **Formatted JSON: {"a":[1,2,{"b":"x y"}],"c":false}**
- **Input size: 45 bytes**
- **Output size: 33 bytes**
- **Nesting depth: 3**
- Nguồn đối chiếu: Python 3.8: json.dumps(json.loads(s), separators=(',', ':')); len(s.encode()) = 45, output 33 bytes

### A bare number is valid JSON (edge)

- JSON:  42 
- Đầu ra: Pretty-print
- Indent with: 2 spaces
- **Formatted JSON: 42**
- **Keys: 0**
- **Nesting depth: 0**
- **Values: 1**
- Nguồn đối chiếu: RFC 8259 §2 (any value may be the top-level text); Python 3.8 json.dumps(json.loads(' 42 '), indent=2) = '42'

### Empty containers stay on one line

- JSON: {"a":[],"b":{}}
- Đầu ra: Pretty-print
- Indent with: 4 spaces
- **Formatted JSON: {
    "a": [],
    "b": {}
}**
- **Keys: 2**
- Nguồn đối chiếu: Python 3.8: json.dumps(json.loads(s), indent=4)

### Large integers are kept digit for digit

- JSON: [12345678901234567890123, -0.5]
- Đầu ra: Minify
- **Formatted JSON: [12345678901234567890123,-0.5]**
- Nguồn đối chiếu: Python 3.8: json.dumps(json.loads(s), separators=(',', ':')) keeps the int exactly (Python ints are arbitrary precision)

### Tabs and sorted keys

- JSON: {"b":1,"a":{"d":true,"c":null}}
- Đầu ra: Pretty-print
- Indent with: Tabs
- Sort keys: có
- **Formatted JSON: {
	"a": {
		"c": null,
		"d": true
	},
	"b": 1
}**
- Nguồn đối chiếu: Python 3.8: json.dumps(json.loads(s), indent='\t', sort_keys=True)

## Câu hỏi

### What are the most common JSON syntax errors?

Trailing commas, single quotes and unquoted keys cause most failures, because JSON requires double quotes around every string and key and allows no comma after the last item. Comments, NaN, Infinity and hex numbers are also invalid under RFC 8259, although JavaScript accepts some of them in code. The error message here gives the line and column, so you can go straight to the character that broke the parse.

### Does JSON allow comments?

No. RFC 8259 and ECMA-404 define no comment syntax, so a standard parser rejects // and /* */. Douglas Crockford removed comments from the format early on because people were using them to hold parser directives. Config files that need comments use JSON5 or JSONC, the format of VS Code's settings.json, and this validator reports those files as invalid JSON.

### How large can a JSON number be?

RFC 8259 sets no limit, but it warns that only integers between −(2^53 − 1) and 2^53 − 1 = 9,007,199,254,740,991 are sure to interoperate, since many parsers use IEEE 754 doubles. This tool copies numbers digit for digit, but JavaScript's JSON.parse turns 12345678901234567890123 into 1.2345678901234568e22, so send large IDs as strings.

### Are duplicate keys allowed in JSON?

RFC 8259 says the names within an object SHOULD be unique, so duplicates are legal but unreliable: JavaScript's JSON.parse and Python's json module keep only the last value, while other parsers keep the first or reject the document. This validator accepts them but warns with the line and column of the object, so you can remove the duplicate before another system reads it differently.

### What is the difference between pretty-printing and minifying JSON?

Pretty-printing puts each member on its own line with 2 or 4 spaces or a tab per level, which makes the data easy to read and diff; minifying removes every space and line break outside strings for transfer. Both describe exactly the same data. The default example grows from 125 to 168 bytes when pretty-printed, and the minify example shrinks from 45 to 33 bytes.

### “JSON formatter and validator” chính xác đến mức nào?

Độ chính xác phụ thuộc vào dữ liệu nhập và giả định của phương pháp. Phép tính thập phân dùng 50 chữ số có nghĩa, nhưng ước lượng, phương pháp số và dữ liệu nguồn có thể kém chính xác hơn; làm tròn khi hiển thị không loại bỏ các giới hạn đó. Ví dụ có lời giải đã đối chiếu với nguồn độc lập: 6. Ví dụ, “Pretty-print with 2 spaces” được kiểm tra bằng Python 3.8: json.dumps(json.loads(s), indent=2, ensure_ascii=False); value count by walking json.loads(s).

### Phương pháp này lấy từ đâu?

RFC 8259 — The JavaScript Object Notation (JSON) Data Interchange Format; ECMA-404 — The JSON data interchange syntax, 2nd edition.

## Nguồn

- [RFC 8259 — The JavaScript Object Notation (JSON) Data Interchange Format](https://www.rfc-editor.org/rfc/rfc8259)
- [ECMA-404 — The JSON data interchange syntax, 2nd edition](https://ecma-international.org/publications-and-standards/standards/ecma-404/)
