Search HoldMyTwist

Browse all posts

Fix JSON errors with JSONLint

Find a trailing comma. Deal with JSON. Understand what a valid result is and what it's not. Short example and real screenshots. Properly handle JSON.

A brass mechanical arm arranging aqua and navy tiles on a workbench

Here is an example of what a json looks like with a trailing comma in the fake json. This type of syntax error could break your import/automation. You can check for syntax errors before you attempt to import after making the fix using JSONLint. Note: Doing this in a browser enviroment using fake data. You don't want to be working with real passwords/tokens/customer data online.

JSONLint (opens in a new tab)

1. Paste a small example

  1. JSONLint (opens in a new tab) 2. Click to enable editor 3. Copy/paste the text below. It has a trailing comma after last value (on purpose). 4. Click on Validate button.

text below:

Text to copy

{
"item": "Notebook",
"quantity": 2,
}

Screenshot adapted from JSONLint, MIT.

JSONLint editor showing a fictional object with a trailing comma after quantity

JSONLint editor showing a fictional object with a trailing comma after quantity

Screenshot adapted from JSONLint, MIT.

Open full-size image

2. Remove the trailing comma

Example: Giving invalid json E008 Line/column closing brace is highlighted. That's where the error is detected.

Look at comma in line just above.

Invalid JSON.

Screenshot adapted from JSONLint, MIT.

JSONLint validation result showing E008 trailing comma in object at line 4 column 1

JSONLint validation result showing E008 trailing comma in object at line 4 column 1

Screenshot adapted from JSONLint, MIT.

Open full-size image
  1. Do not comma after 2. (Leave the comma after "Notebook". You got another field after it) 2. Press the validate button again

Watch out

Valid JSON is a JSON conforming to the syntax. It doesn't mean the address is valid, or that the order is there, or that that particular app will expect those fields to be there. Be sure to check what fields they expect where, and what should be in those fields. Before attempting to load/submit data.

3. Copy the corrected result

The corrected object returns Valid JSON. It contains two fields: item with a text value and quantity with a number value.

Text to copy

{
"item": "Notebook",
"quantity": 2
}

Screenshot adapted from JSONLint, MIT.

JSONLint result showing Valid JSON after the trailing comma is removed

JSONLint result showing Valid JSON after the trailing comma is removed

Screenshot adapted from JSONLint, MIT.

Open full-size image

The last JSON object is where we have the "quantity": 2 and the } for that object. No comma after that last numeric 2 before the }. There should be a comma after "Notebook" since it is the previous item property. Before the quantity property.

You can either copy that corrected text to copy to the JSON field of destination or you can use a plain text editor. The numeric 2 won't be quoted if you are copying to a destination that expects a number. "2" is text. Try a harmless record before a big file.

If validation still fails

First get the one that has been reported. Then the one before that. Use regular double quotes for field names and text. Use commas to separate the fields. Close of braces or brackets. You can see what's possible here JSON syntax reference (opens in a new tab).

Put the original in some storage. If you're going to keep going, for the auto-repair, see what the repaired values are, compared to the original. Syntax repair has no idea what you're trying to say.

Sources: JSON.org (opens in a new tab); JSONLint privacy policy (opens in a new tab); JSONLint source and licence (opens in a new tab). Screenshots show JSONLint with fictional data and are cropped to the relevant controls.

JSONLint source (opens in a new tab). Licence and screenshot notices.

Share this post