realnumber
realnumber

Reputation: 2284

JSON.parse: expected property name or '}'

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

Answers (12)

ensiye karimi
ensiye karimi

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

sitanosik
sitanosik

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: enter image description here

Removing whitespace has fixed the issue: enter image description here

Upvotes: 2

FabioSSena
FabioSSena

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

Praphant Srimonthok
Praphant Srimonthok

Reputation: 71

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

Upvotes: 1

Moh Misaghi
Moh Misaghi

Reputation: 31

You can try using stringifying before parsing: JSON.parse(JSON.stringify(result))

Upvotes: 3

Peterson Aquino
Peterson Aquino

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

alchemication
alchemication

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

nativelectronic
nativelectronic

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

Dazeh
Dazeh

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

kheengz
kheengz

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

Jonathan M
Jonathan M

Reputation: 17451

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

myJSONstring.replace(/&quot;/ig,'"');

Upvotes: 38

Related Questions