Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To write JSON that works reliably, follow three checks: conform to JSON’s grammar, parse the text with a standards-oriented parser, and validate the parsed data against the receiving application’s requirements. Passing a syntax check is necessary, but it does not guarantee that the data has the right structure or will behave consistently across systems.

What JSON is—and what counts as a JSON value

JSON is a lightweight, language-independent text format for representing structured data, defined by RFC 8259. Its values include objects, arrays, strings, numbers, the lowercase literals true and false, and null.

An object is a collection of name/value pairs; an array is an ordered sequence of values. A JSON text can contain any JSON value at its top level, including a string, number, boolean, or null. An object or array is a conservative choice when sending data to older or constrained systems that may expect one of those structures.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What does valid JSON look like?

JSON has a small, strict grammar. It is not the same as a JavaScript object literal, even if the two can look similar.

  • Objects use braces: {}. Each name is a double-quoted string, followed by a colon and a value.
  • Arrays use brackets: []. Commas separate their values.
  • Commas separate object members too, but there is no comma after the final member or array element.
  • Whitespace may appear around structural characters.
  • Strings and object names use double quotation marks. Quotation marks, reverse solidus characters, and control characters must be escaped as required by the grammar.
  • Numbers use JSON’s decimal syntax. A number cannot have a leading zero unless it is zero itself; fractions and exponents are allowed in the defined forms.

This is valid JSON:

{
  "name": "Ada",
  "active": true,
  "roles": ["editor", "reviewer"],
  "score": 9.5,
  "nickname": null
}

Standard JSON does not include comments, single-quoted strings, unquoted object names, trailing commas, undefined, NaN, or infinity values. Some tools accept one or more of these as extensions, but a generator intended to produce JSON should emit text that follows the standard grammar.

How to write and check JSON reliably

  1. Choose the data shape first. Decide which values are objects, arrays, strings, numbers, booleans, or null. Use double quotes for every string and object name.
  2. Check punctuation as you build. Pair every opening brace or bracket with a closing one. Separate members and elements with commas, but omit the comma after the last one.
  3. Parse the finished text. A parser catches syntax errors such as missing commas, mismatched brackets, and invalid escapes. For example, run python -m json.tool file.json in a terminal with Python available. It validates and pretty-prints valid JSON; invalid input produces a JSONDecodeError with a location, including line and column information. See the Python 3.12.15 JSON documentation for its command-line and error behavior.
  4. Validate the data’s meaning separately. Compare it with the contract expected by the receiving application, or use a suitable JSON Schema. JSON Schema is a declarative language for describing and validating document structure, constraints, and data types. A schema can check requirements that grammar alone cannot, such as whether a required field exists or a value has the expected type.
  5. Check the intended environment. Test with the parser, settings, and schema version the receiving application uses. Acceptance by one parser is not proof that every parser will accept the same text or interpret it the same way.

Why is my JSON invalid?

Parser messages often point near the problem, but the underlying mistake may be just before the reported position. Check the punctuation and quoting around the location as well as the indicated character.

Symptom Likely cause What to check
“Expecting property name enclosed in double quotes” An object name is unquoted or uses single quotes. Write the name as a double-quoted string, followed by a colon.
A failure near the end of an object or array A missing comma, mismatched brace or bracket, or trailing comma. Match every opening delimiter, separate entries with commas, and remove any comma after the final entry.
Parsing succeeds, but the application rejects the data The JSON is syntactically valid but does not meet the application’s expected structure or constraints. Check the application contract or validate against an appropriate schema.
Accepted locally but rejected elsewhere The local parser may accept extensions, or another implementation may handle duplicate object names differently. Use standard JSON syntax, remove duplicate names, and avoid nonstandard values.

How to make JSON more portable

Keep object names unique

Each name in an object should be unique. RFC 8259 warns that behavior for duplicate names is unpredictable across implementations: a parser might keep the last value, reject the object, or expose the duplicates. Do not rely on one parser’s behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Avoid nonstandard values and parser extensions

A tool that accepts comments, trailing commas, NaN, or infinity is accepting more than standard JSON. Such input may fail when another system parses it. Use an extension only when the entire data path explicitly supports it; otherwise, emit standard JSON.

Consider numeric precision and implementation limits

JSON’s number grammar does not guarantee that every receiver can represent every number precisely. RFC 8259 discusses interoperability limits in numeric range and precision; for example, a long integer identifier may be changed when processed by a system whose number handling cannot preserve it. If a value is an identifier rather than a quantity for arithmetic, consider representing it as a string when the receiving contract permits that.

Implementations may also limit accepted input size, nesting depth, numeric range or precision, and string size. Do not assume that a document accepted by one runtime can be processed at any size or depth by another.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What a parser check does—and does not—prove

A successful parse establishes that the parser accepted the text as JSON under its rules. It does not establish that required fields are present, that values meet application constraints, or that another parser will interpret the document identically. For dependable interchange, combine syntax parsing with schema or contract validation, and avoid duplicate names and nonstandard extensions.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.