Check AI JSON with a real parser before you keep it

Write the object you want first. Parse the reply with JSON.parse or a strict json.loads. Keep it only when the values match.

Published 2026-11-25 · Translation: VI

In short

  • Write the JSON object you want before you read the chat reply.
  • Parse with Node JSON.parse, or Python json.loads with parse_constant and object_pairs_hook so NaN and repeated keys fail.
  • Keep the reply only when parse succeeds and every name and value matches what you wrote.

Example (made up): a shop expects one rice line as JSON — {"sku":"RICE-5KG","qty":2,"note":null}. ChatGPT or Claude only produces text. Your parser decides whether that text is the object you asked for.

Same idea as testing a regex on rows you already marked, or a SQL NOT IN on a fixture with one NULL.

Valid JSON is not enough

Checked on Python 3.12.3 and Node v22.23.4 (25 November 2026).

Whitespace and reordered keys are fine — RFC 8259 allows insignificant spaces, and object equality does not care about key order. A bare 2 also parses, but it is not your object. Two JSON values stuck together fail (Extra data / Unexpected non-whitespace character).

These replies parse, but they are not your object unless you compare:

Reply What you get Matches expected?
"qty": "2" string, not number No
extra "ok": true four keys No
note omitted two keys No
"qty": 9 then "qty": 2 one qty = 2 Looks equal — still discard

Repeated keys are the trap. RFC 8259 §4 says names SHOULD be unique; many parsers keep only the last value. Default Python/JSON.parse can make "qty": 9, "qty": 2 look identical to your object. Use object_pairs_hook (Python) or an explicit duplicate-key check so the repeat fails.

Expected: sku RICE-5KG, qty as number 2, note null. Three replies still parse: qty as string 2, an extra ok, and qty written twice ending in 2. Discard all three.

With object_pairs_hook, the repeated-qty reply raises ValueError: repeated name: qty. Plain == never sees the 9.

Trailing commas and NaN are not JSON

JSON does not allow a comma after the last field. This text fails both parsers:

{"sku":"RICE-5KG","qty":2,"note":null,}

Python: JSONDecodeError at column 39. Node: SyntaxError at position 38. The message asks for a property name — it does not say “trailing comma.”

NaN / Infinity are also not JSON. Default Python json.loads still accepts them; Node throws. Pass parse_constant that raises so Python matches Node.

Do not use eval() on chat JSON (RFC 8259 §12 — security risk). Use JSON.parse / json.loads. Python ast.literal_eval is a different language (needs None, not null).

Trailing comma stops both parsers near the final brace. Code fences and single quotes fail early. qty NaN: Node throws; default json.loads returns nan until parse_constant raises.

Python’s docs call that nan result outside the JSON spec.

Ask the chat, then verify yourself

Return one JSON object and nothing else.
Object I already wrote:
{"sku":"RICE-5KG","qty":2,"note":null}
qty is a number. note is null. Each name appears once.
I will parse with JSON.parse and with json.loads
using parse_constant and object_pairs_hook.
A fence, trailing comma, repeated name, or different value fails.

Strict Python loader:

def refuse_constant(text):
    raise ValueError(text)

def refuse_repeat(pairs):
    names = [name for name, _ in pairs]
    repeated = sorted({name for name in names if names.count(name) > 1})
    if repeated:
        raise ValueError("repeated name: " + ",".join(repeated))
    return dict(pairs)

def load(text):
    return json.loads(
        text,
        parse_constant=refuse_constant,
        object_pairs_hook=refuse_repeat,
    )

Node field check (still add a duplicate-key check — JSON.parse keeps the last value):

function same(sheet, reply) {
  const a = JSON.parse(sheet);
  const b = JSON.parse(reply);
  const names = Object.keys(a);
  if (Object.keys(b).length !== names.length) return false;
  return names.every((k) => Object.hasOwn(b, k) && b[k] === a[k]);
}

Parse, then compare fields. Keep only when names and values match and no name repeats. Anything else → ask again from the object you wrote.

Pretty-printing and reordered keys still pass. Failures are wrong values, repeats, or parse errors.

Watch-outs

  • 2.0 and 2e0 equal 2. Accept as number 2 unless you need the exact spelling.
  • Commas inside strings are fine. {"note":"5 kg, dry"} parses; then compare note to your expected null.
  • These never reach compare: single quotes, unquoted keys, // comments, +2 / 02 / True / None, smart quotes, leading prose, ```json fences.
  • U+2028 is not JSON whitespace between tokens (both parsers stop). Inside a string it is allowed.
  • Leading BOM (U+FEFF) fails both parsers.
  • Another library may accept extensions. Re-run on the machine that will keep the object.

Share

X Facebook LinkedIn
Educational content only. Rankings reflect stated criteria at the update date—not investment advice or permanent performance guarantees.