43
votes

Data contains (/"/):

 {"test":"101","mr":"103","bishop":"102"}

script:

console.log($.parseJSON(result));

I'm getting error,

JSON.parse: expected property name or '}'.
5
Based on error message, maybe you have an unquoted apostrophe in there? Those must be escaped using backslash, or Unicode escape (\u0027) - StaxMan
sorry thats a type, there i only have " - realnumber
Your JSON as posted is clean. Please edit to reflect actual JSON string if it's different. - Jonathan M
What do you mean by "Data contains (/&quot/)"? - Jacob
{"Chairman of the Board":"1","General Manager":"10"} this what i get . - realnumber

5 Answers

80
votes

Had same issue when used single quotes in JSON file, changed to double quotes for all string properties/values and it's working OK now, hope it helps anyone....

Change:

JSON.parse("{'wrongQuotes': 5}") 

To:

JSON.parse('{"rightQuotes": 5}')
24
votes

If you're receiving the JSON with the encoded ", you'll have to replace each instance of " with a true " before doing JSON.parse. Something like:

myJSONstring.replace(/"/ig,'"');
3
votes

For anyone who is using laravel blade and declaring a JS variable in a view.

You need to use:

var json = JSON.parse('{!! $json !!}');

Otherwise you will get this error due to the quotes being parsed as "

2
votes

Change
{"test":"101","mr":"103","bishop":"102"}
To
'{"test":"101","mr":"103","bishop":"102"}'

if this is coming from the server (PHP)
i.e <?php $php_var = ["test" => "101", "mr" => "103", "bishop" => "102"]?>

then on Javascript end
var javascript_var = $.parseJSON('<?= json_encode($php_var) ?>');

0
votes

for example, if u get something like this

{ "location": "{'lat': 4.6351144, 'lng': -74.12011199999999}" }

from your server, or recently get a bad converted format. first,get your string

myItemString = "{'lat': 4.6351144, 'lng': -74.12011199999999}"

and change the keys using replace, and later json.parse, 'key' to ---> "key"

     const key1 = myItemString.replace("'lat'",'"lat"')
      const key12 = key1.replace("'lng'", '"lng"');
      const obj = JSON.parse(key12)
      console.log(obj)