JSON.parse() — Watch your Tail.

Vilva Athiban P B
2 min readSep 7, 2018

With JavaScript Release 1.8.5, we are in a situation to watch our tails when we use JSON.parse(). The following feature may not be a game changer, but if not properly taken care, might end up being lethal to the application.

Trailing commas:

It is considered to add Trailing comma during object creation, as below, for two reasons:

  1. When we add a new property, it will be easy to add it in new line
  2. Also, version control wont create a diff when we add a new property.
let sampleObject = {  
id : "ABC123",
name: "Test Name",
age : 25,
}

JSON.parse() :

On the other hand, JSON.parse() doesn’t support trailing commas in the stringified Object. When we try to parse a Stringified Object with trailing comma using JSON.parse(), we end up in error as below screenshots.

This might be really a serious issue if a Stringified Object with trailing comma is sent from the backend API response and might break the application.

JSON.stringify() comes to the rescue:

--

--