Srinivas B
Srinivas B

Reputation: 1852

Unexpected error while parsing JSON in javascript

I am getting a string object after splitting from a text. Now I am trying to convert that text to a JSON object.

Text after Splitting

{location:"Web",initial:"",firmType:"",toaxfrtype:""}

When I am trying to parse it using JSON.parse, I am getting an error,

SyntaxError: Unexpected token l.

I have some other string value as the same in the above text. It was parsing fine using JSON.parse. Only the above string is not working. Can anybody help me in this issue.

Upvotes: 0

Views: 1211

Answers (2)

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

You need quotes " so change

 {location:"Web",initial:"",firmType:"",toaxfrtype:""}

to

 {"location":"Web","initial":"","firmType":"","toaxfrtype":""}

I have attached the image where it says valid now after correction. You can online validators to validate it first.

enter image description here

Upvotes: 3

Thilo
Thilo

Reputation: 262504

This is not JSON. You need to quote all Strings, including the keys.

Upvotes: 0

Related Questions