1

I have this variable which is json-like string, error appear while parse to a object

"SyntaxError: JSON.parse: expected ',' or '}' after property value in object"

Code:

var obj = JSON.parse('{"data":[{"from":"{\"category\":\"Bank/financial institution\"}"}],"statusCode":200}');

Seems the function not available for nested "{\"category\":\"Bank/financial institution\"}", replace with simple text (e.g. "123") would be fine, is there any way to handle such cases?
Thanks.

4
  • String returned from Java Program. Tried to remove "\" but no luck Commented Dec 19, 2012 at 9:45
  • @mplungjan: Assuming you meant str = str.replace(/\\/g, "") (replace has no third argument), that would actually make things worse. :-) You have to remove the quotes around from's value as well. Commented Dec 19, 2012 at 9:58
  • @mplungjan: No, it doesn't, not in the standard. And it doesn't work on Chrome, so apparently it's not a broadly-supported non-standard thing. Commented Dec 19, 2012 at 10:05
  • It is not standard and does not work in Chrome. I never use it anyway, but was lazy with the \/\/ - I have deleted the comment anyway Commented Dec 19, 2012 at 10:09

3 Answers 3

3

The \ (backslash) character before "category is unnecessary.

There's no need to escape a double quote in a single-quoted string.

Sign up to request clarification or add additional context in comments.

2 Comments

I think from is a string value not an object.
@VisioN - yes, I edited from invalid to unnecessary (almost straight away)
2

Your string is indeed malformed.

You either want:

var obj = JSON.parse('{"data":[{"from":{"category":"Bank/financial institution"}}],"statusCode":200}');

...(e.g., without quotes around the value of from and without backslashes) which when deserialized results in an object with a property called data which is an array, which has as its first entry an object with a property called from which is an object: Live Example | Source

or

var obj = JSON.parse('{"data":[{"from":"{\\"category\\":\\"Bank/financial institution\\"}"}],"statusCode":200}');

...(e.g., keeping the quotes around the value for from and making sure the backslashes appear in the JSON, which means escaping them) which is the same until you get to from, which is a string: Live Example | Source

Comments

1

Remove quots for inner object

var obj = {
    "data": [{
        "from": {
            "category": "Bank/financial institution"
         }
     }],
     "statusCode": 200
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.