Urgent.News

What's breaking now, across thousands of outlets.

Tech

5 reasons your JSON fails to parse (and how to fix each one)

If a parser throws Unexpected token , the cause is almost always one of five things. 1. Trailing comma JSON does not allow a comma after the last item, even though JavaScript does. { "name" : "Ana" , "age" : 30 , } Remove the last comma. 2. Single quotes Keys and strings must use double quotes. {'name': 'Ana'} is a JavaScript object, not valid JSON. 3. Unquoted keys {name: "Ana"} fails. Every key…

When a parser encounters an unexpected comma, the issue is usually one of five common mistakes. First, JSON does not permit a trailing comma after the last item, even though JavaScript allows it. For example, removing the final comma from `{ name : Ana , age : 30 }` resolves the error.

Second, keys and strings in JSON must be enclosed in double quotes. An object like `{ name : Ana }` is a valid JavaScript object, but it is not valid JSON syntax. Changing the quotes to double quotes makes it a proper JSON object.

Third, keys in JSON cannot be unquoted. An object like `{name: Ana }` will fail to parse because every key requires double quotes. Fixing this by adding the missing quotes will typically resolve the parsing problem.

Fourth, JSON has no support for comments. Any `//` or `/* */` comments will cause parsing to fail. Before attempting to parse the data, remove any comments from the JSON string.

Fifth, invisible characters at the beginning of the file can sometimes cause problems. A Byte Order Mark (BOM) or a line number that was inadvertently pasted can interfere with the first token in the JSON data. If the error indicates position 0 and the file appears correct, inspect the data for these types of invisible characters. Use a formatter available online to pinpoint the exact location. Once identified, make the necessary correction and rerun the parsing.

If you are parsing data obtained from a log or a Python print statement, a thorough inspection and cleanup of quotes and trailing commas can save a significant amount of manual editing time. Two free tools are available to assist with this process. The Formatter at https://snipferry.com/json/formatter and the Repair tool at https://snipferry.com/json/repair both operate directly in your web browser, ensuring that your original data remains confidential.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

PDF Invoice Template Recovery: 3 Audit Records Across Layout Changes

Choose the layout owner before designing recovery: keep invoice HTML in the repository when engineers change it, and choose a stored template when finance needs to edit it without a deploy.

  • Store original scan, OCR result, and final PDF as separate artifacts
  • Associate each invoice with exact layout revision used
  • Utilize Infrai REST API for storage-to-document processing

title: Python Day 1 – Why Python, and What Even Is It?

So I started a Python course, and I'm going to write down what I learn each day here. Mostly for me, but maybe it helps someone else too. What is Python?

  • Python is a high-level, general-purpose programming language
  • Created in 1991, Python 3 released in 2008
  • Python used in data science, web development, and automation

More from Saturday 14 November →