
Fixing JSON That Is Really a JavaScript Object Literal
Text copied from console.log, DevTools or a config file is often a JavaScript object, not JSON. What differs, what is lost, and how to convert it safely.
Everything you need to level up your coding skills.

Text copied from console.log, DevTools or a config file is often a JavaScript object, not JSON. What differs, what is lost, and how to convert it safely.

Leading zeros, hex, base 60 times and trailing zeros change what a number is. Real output for 0755, 12:30, 1.10 and 1e3, and how to protect values.

A response or file that stops mid-document fails with an end-of-input error. How to confirm truncation, salvage complete records and decide on repair.

A raw newline, tab or backslash inside a JSON string is invalid. See the errors Python and Node give, how strict=False helps and how to write strings safely.

A line with a word and no colon makes the parser fail while scanning a simple key. See the message in three parsers, the usual causes and the fixes.

When a minified XML file will not parse, xmllint --format and lxml refuse to indent it. A 30-line tokenizer indents what it can and flags the first mismatch.

A UTF-8 byte order mark in front of JSON breaks strict parsers in Python and Node. What the three bytes are, who adds them and how to read or remove them.

When an XML feed mixes encodings or was encoded twice: why detectors guess wrong on short text, the UTF-8 first rule and per-line fallback.

Why a line indented between two levels raises did not find expected key, how PyYAML words it differently, and how to realign. Output from three parsers.

Extra data means a valid JSON value was followed by more text. Why concatenated objects and JSON Lines trigger it and three ways to read them in Python.

Repair a 29 MB XML export with 215,385 raw ampersands using a lookahead regex that leaves existing entities alone, with the cases that need judgment.

Why json.loads or response.json() raises Expecting value at line 1 column 1 in Python, how to see what the text really is, and a checklist for each cause.
We send one email when a new article is published. No spam.