Fixing “unexpected token in JSON” error in Nodejs Javascript

Sep 2020, Gorakhpur, India
blog

Error while parsing JSON

So, I was working on a project where I created an API that takes Users Location and Shops Location as a request parameter and returns the distance from the user under a certain diameter (Yes, the same functionality as used by UBER/OLA to tell us the nearest cabs or Swiggy/Zomato to tell us the nearest restaurants).

Here is the parameter I was sending as Body in Postman

shop = [ { latitude: 26.759362, longitude: 83.377485, name: “Dominoes” }, { latitude: 26.753586, longitude: 83.378468, name: “Rangrezza” }, { latitude: 26.752640,longitude: 83.370958,name: “Shahanshah” }, { latitude: 26.852785,longitude: 80.999626,name: “Royal Cafe” }, { latitude: 26.857992,longitude: 80.947613,name: “Mummas Cafe” }, { latitude: 28.672742,longitude: 77.452321,name: “Maharani Kitchen”}, { latitude: 28.677797,longitude: 77.442009,name: “Handi Dining”}, ]

user = [{“key”:”user”,”value”:”{ latitude: 100, longitude: 100 }”,”description”:””,”type”:”text”,”enabled”:true}]

After testing everything through hardcoded values when I finally moved forward and made an API, The JSON that I was sending in Body as Form Data was not getting Parsed using JSON.parse neither getting converted using JSON.stringify.

I was always getting an error of “Unexpected token l in JSON at position 4”

After doing a lot of “RIGHT” google search I came across this amazing library named as Really-Relaxed-JSON. It helps you in certain cases -

  • Commas are optional between objects pairs and array items.
  • Trailing commas are allowed.
  • Quotes are optional around simple-keys and simple-values.
  • Single-quote pairs, double-quote pairs, and backtick pairs can be used for strings.
  • Single-line and multi-line comments are allowed.
  • Multiline strings are allowed.

Lets Talk about some code implementation now. Import Really Relaxed JSON as a constant using const parser = require( ‘really-relaxed-json’ ).createParser();

blog
Using Really Relaxed JSON to Parse typical JS objects sent as Params.

The following code parses the data I mentioned above and gives this as a result -

blog
index.js sample socket-io server

The code prints 1) The req.body that I get via postman (also mentioned above). 2) The string that we get using Really Relaxed JSON. 3) The string in step 2 parse to JSON again to be used in the code (despite all the rules we are not following for the general JSON object).

This is how I was able to get this API up and running quickly.

This is a Quick and Hacky solution, not recommended to use but it helps brush up your concepts on Javascript Objects Notation. Deadlines also sometimes play an important role in getting things done and improve the solution over time.

I figured this out some years back when I was just intermediate in Nodejs.

star

Are you ready
to bring your
vision to life?

The journey starts here. We're excited
to meet you.

Check us out on Glassdoor.