JSON.parse() — Watch your Tail.

Vilva Athiban P B
Sep 7, 2018 · 2 min read

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:

Though its a serious issue, when we use JavaScript to form the Stringified object, we can ignore this. JSON.stringify() method, by default, ignores the trailing commas, if any.

Though it is not a very big issue, a little awareness about this feature of JavaScript might be a life-saver for someone in such rare issue. Hope the article is useful. Thanks for reading.

Vilva Athiban P B

Written by

4+ years of Experience in React-Redux / NodeJS / GraphQL. Companies and Devs from Europe? Lets talk — https://www.linkedin.com/in/vilvaathiban/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade