Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is JSON.Parse saying "invalid character"?

I have a piece of JS running in IE with the following line:

var data = JSON.parse("{ skill: 'SK_AUTO_DEV_TEST', kind: 'IS_REQUIRED' }");

Can anyone tell me what's wrong with this?

like image 707
skb Avatar asked Dec 10 '13 16:12

skb


People also ask

How to escape invalid characters from a JSON field?

The Description field I was passing has characters that JSON considers invalid characters. In order to use those characters they have to be escaped. typically with a backslash. Anyway, it was simple replace operation, although I used multiple compose.

Is the string 36151b9e-ad0d-49de-a14b-5461489c7065 invalid JSON?

36151b9e-ad0d-49de-a14b-5461489c7065 is invalid JSON. Maybe you meant "36151b9e-ad0d-49de-a14b-5461489c7065", which is valid JSON. It appears that you are trying to parse a string that is not valid JSON.

Why is This JSON file showing as valid?

This JSON in particular shows as valid. There's a chance that your particular client/library that handles the JSON that gets sent to ModSecurity is screwing up something and breaking the JSON.

Why do I need quotes around the property names in JSON?

Because that is not Valid JSON – you need quotes around the property names. To elaborate on epascarello's answer, please refer to json.org. Note in the first diagram that the name in the name/value pair is defined as "string". Then note in the diagram that defines "string" that it must begin and end with double-quote.


2 Answers

Because that is not Valid JSON – you need quotes around the property names.

JSON.parse('{ "skill": "SK_AUTO_DEV_TEST", "kind": "IS_REQUIRED" }');
like image 130
epascarello Avatar answered Sep 21 '22 00:09

epascarello


To elaborate on epascarello's answer, please refer to json.org. Note in the first diagram that the name in the name/value pair is defined as "string". Then note in the diagram that defines "string" that it must begin and end with double-quote. Also note that this applies to both name and value.

like image 24
Jack A. Avatar answered Sep 21 '22 00:09

Jack A.