Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk5 min

JSON Essentials: How to Write, Parse, and Validate It

A practical guide to writing standard JSON, parsing it for syntax errors, validating it against application requirements, and avoiding common interoperability problems.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To know whether JSON will work, check it in two stages: parse it to confirm the text follows JSON syntax, then validate it against the receiving application’s requirements. A document can be valid JSON yet still have missing fields, incorrect types, or values another system handles differently.

What does valid JSON look like?

JSON is a lightweight, text-based format for exchanging structured data. Its grammar is defined by RFC 8259. JSON can represent objects, arrays, strings, numbers, the lowercase literals true and false, and null.

As an Amazon Associate I earn from qualifying purchases.

  • Objects use braces, {}, to hold name-and-value pairs. Each name is a string followed by a colon and a value.
  • Arrays use brackets, [], to hold an ordered sequence of values.
  • Strings and object names use double quotes. Required characters, including quotation marks, reverse solidus characters, and control characters, must be escaped.
  • Numbers use JSON’s decimal syntax, which permits fractions and exponents but not leading zeros before nonzero digits.

For example, this is a JSON object containing strings, a number, a boolean, an array, and a null value:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
{
  "name": "Ari",
  "items": ["book", "lamp"],
  "active": true,
  "score": 12.5,
  "note": null
}

JSON is not the same as JavaScript object-literal syntax. Standard JSON has no comments, single-quoted strings, unquoted object names, trailing commas, undefined, NaN, or infinity values. Some parsers accept such extensions, but their acceptance does not make the text standard JSON.

RFC 8259 allows a JSON text to contain any JSON value, including a top-level string, number, boolean, or null. An object or array at the top level may be the more conservative choice when exchanging data with older or constrained systems.

How do you write JSON reliably?

  1. Decide what each value represents. Use objects for named properties, arrays for ordered lists, strings for text, numbers for numeric values, booleans for true-or-false values, and null when the data calls for that explicit value.
  2. Quote names and text with double quotes. Put a colon between each object name and value. Separate members and array elements with commas, but do not put a comma after the final item.
  3. Match the structure. Close every opening brace and bracket with its corresponding closing character. Whitespace may appear around structural characters.
  4. Use only JSON values. Do not add comments or rely on JavaScript-only values such as undefined or NaN.
  5. Keep object names unique. Repeating a name in one object can produce different outcomes across implementations.

How do you check whether JSON is valid?

Parse it with a JSON parser rather than relying on visual inspection. Parsing catches syntax problems such as missing commas, mismatched braces, malformed escapes, or incorrectly quoted names. It does not establish that the data meets an application’s requirements.

Check JSON from the command line with Python

Python’s standard-library JSON tool can parse a file and pretty-print it:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
python -m json.tool input.json

Replace input.json with the path to your file. If parsing succeeds, the tool prints formatted JSON. If it fails, Python reports a JSONDecodeError with the error position, line, and column, which can help locate the syntax problem. See the Python 3.12.15 JSON documentation for the module’s behavior.

A successful parse means the parser accepted the text; it does not prove every other parser will interpret it identically or that the receiving application will accept its contents.

How do you validate JSON against application requirements?

After parsing, check the data against the contract expected by the receiving application. That contract may require specific properties, types, or constraints. For example, an application may require a string property named email, even though JSON syntax itself does not require that property to exist.

JSON Schema is a declarative way to describe and validate JSON document structure, constraints, and data types. Use a schema that matches the intended contract and the schema version supported by your validator. A schema check answers a different question from parsing: whether the parsed value has the required shape and constraints, rather than whether its text is grammatical JSON.

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

Why is my JSON invalid?

Symptom Likely cause What to check
“Expecting property name enclosed in double quotes” An object name is unquoted or uses single quotes. Use a double-quoted string for every object name.
A parser fails near the end of an object or array A comma or closing bracket may be missing, or a trailing comma may be present. Check that each opening brace or bracket is matched and that commas separate entries only—not the last entry from the closing character.
The text works locally but fails in another system The local parser may accept nonstandard extensions, or duplicate names may be handled differently. Use standard JSON syntax and remove duplicate names and nonstandard values.
Parsing succeeds, but the application rejects the data The JSON is syntactically valid but does not meet the application’s structure or constraints. Validate against the receiving contract or an appropriate JSON Schema.
A long numeric identifier changes after processing A receiving system may not preserve the number’s full range or precision. Check the number handling of every system in the data path; for identifiers that must be preserved exactly, consider representing them as strings if the application contract permits it.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What can make valid JSON behave differently across systems?

Duplicate object names

Object names should be unique. RFC 8259 warns that behavior for duplicate names is unpredictable: an implementation might keep the last value, reject the object, or expose duplicates. Python 3.12.15’s standard-library decoder, for example, uses last-name-wins handling by default. Do not depend on one parser’s choice when another system may make a different one.

Nonstandard parser extensions

Python 3.12.15 documents default acceptance and output of NaN and infinities, even though those values are outside JSON’s number grammar. A successful local parse or serialization involving them therefore does not guarantee interoperable JSON. Prefer values conforming to RFC 8259 when data will cross system boundaries.

Number range and precision

JSON defines number syntax but does not require every implementation to preserve every possible numeric value with unlimited range and precision. RFC 8259 calls out interoperability limits, including common binary64-based handling. Large integers—particularly identifiers—can be risky if consumers use different numeric representations. Confirm the receiving systems’ behavior, or use strings for exact identifiers when the application’s contract allows it.

Size and nesting limits

Implementations may limit accepted document size, nesting depth, number range or precision, and string length. RFC 8259 does not guarantee that every parser accepts arbitrarily large or deeply nested input. If data approaches those limits, test it with the actual parsers and settings used by the systems that exchange it.

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.

What is the dependable JSON workflow?

  1. Write the data in standard JSON syntax. Use double quotes, lowercase literals, valid number syntax, commas between entries, and no comments or trailing commas.
  2. Parse it with a JSON parser. Fix syntax errors reported by the parser instead of assuming a document that looks right is valid.
  3. Validate its meaning. Check required fields, types, and constraints against the application contract or a suitable JSON Schema.
  4. Check interoperability. Avoid duplicate names and nonstandard values, and confirm that numeric precision, document size, and nesting depth are suitable for the intended systems.
  5. Test with the intended environment. Use the parser, its settings, and the schema version that the receiving application actually supports.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.