Dan Fisher
Dan Fisher

Reputation: 41

JSON.parse not able to parse valid JSON object

JSON.parse throws error when I try to parse a valid JSON object. My issue is i'm receiving data from webservice and sometimes it works with parse and sometimes it doesn't not sure what is the reason why. I would expect JSON.parse to return same object if its a valid JSON object? or parse it if its a string.

 var obj1= { Result: Inprogress };
 var json = JSON.parse(obj1);

please help me with this understanding

Upvotes: 0

Views: 2767

Answers (2)

castel YOUDOM
castel YOUDOM

Reputation: 1

Your JSON is malformed. A right JSON is

{ "Result": "Inprogress" }

You have forgotten the "" character in beginning and in end of each part of your JSON.

Upvotes: 0

Pointy
Pointy

Reputation: 414016

What you have there is a JavaScript object. It doesn't need to be parsed because it's plain JavaScript syntax and JavaScript itself parses it. JSON is a serialization format.

The JSON.parse() method takes a string argument, like one retrieved from an ajax call or from local storage or some other source of data that only deals in string values.

Upvotes: 2

Related Questions