Reputation: 2284
Data contains (/"
/):
{"test":"101","mr":"103","bishop":"102"}
script:
console.log($.parseJSON(result));
I'm getting error,
JSON.parse: expected property name or '}'.
Upvotes: 85
Views: 217495
Reputation: 1
I was encountered this error too. In my case, I had built "db.js" instead of "db.json" by mistake. The prefix was wrong!. As a result,JSON.parse() wouldn't work.
Upvotes: 0
Reputation: 43
I copied my JSON object from password vault to Postman which messed up formatting somehow - whitespace around brackets was causing the JSON.parse error:
Removing whitespace has fixed the issue:
Upvotes: 2
Reputation: 41
let lng_lat = '{lng:-50.31756617529646,lat:-27.168027359667732}';
let converted = JSON.parse(lng_lat.replaceAll(':', '":"').replaceAll('{','{"').replaceAll('}','"}').replaceAll(',','","'));
console.log(converted);
// Or
lng_lat = lng_lat.replaceAll(':', '":"');
lng_lat = lng_lat.replaceAll('{','{"');
lng_lat = lng_lat.replaceAll('}','"}');
lng_lat = lng_lat.replaceAll(',','","');
converted = JSON.parse(lng_lat);
console.log(converted);
Upvotes: 1
Reputation: 31
You can try using stringifying before parsing:
JSON.parse(JSON.stringify(result))
Upvotes: 3
Reputation: 31
/* suppose your json are single quote, it's necessary replace it single quote before, a simple example*/
let ojson = "{'name':'peterson'}";
ojson = ojson.replace(/'/g, '"');
ojson = JSON.parse(ojson);
console.log(ojson['name'])
Upvotes: 3
Reputation: 5324
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.
Change:
JSON.parse("{'wrongQuotes': 5}")
To:
JSON.parse('{"rightQuotes": 5}')
Upvotes: 151
Reputation: 5266
My case was even simpler.
Having confused JSON with plain JS, I didn't put the object keys in double quotes.
❌:
{
title: "Hello World!"
}
✅:
{
"title": "Hello World!"
}
The irony is, Postman even highlighted this to me, but I ignored. Sleep is the answer.
Upvotes: 21
Reputation: 862
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)
Upvotes: 0
Reputation: 316
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 "
Upvotes: 8
Reputation: 1038
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) ?>')
;
Upvotes: 3
Reputation: 17451
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,'"');
Upvotes: 38