2 votes

JSON.parse: expected property name or '}' JavaScript

I have this String:

[
    {'lat':43.4627,'lng':-3.79636,'description':'a'}, 
    {'lat':43.4476,'lng':-3.82948,'description':'b'}, 
    {'lat':43.4228,'lng':-3.82391,'description':'c'}
]

And when I try to validate it here I get this error:

SyntaxError: JSON.parse: expected property name or '}' at line 1 
column 2 of the JSON data

Can anyone tell me what is wrong with my String?

2voto

Elenasys Points 67941

The error you mention

JSON.parse: expected property name or '}'

or you can also obtain :

Expecting 'STRING', '}', got 'undefined'.

They are caused by the need to use double quotes to define strings.

This would work correctly:

[{
        "lat": 43.4627,
        "lng": -3.79636,
        "description": "a"
    },
    {
        "lat": 43.4476,
        "lng": -3.82948,
        "description": "b"
    },
    {
        "lat": 43.4228,
        "lng": -3.82391,
        "description": "c"
    }
]

I recommend using a json validator in these cases.

1voto

The object field names must be double-quoted instead of single-quoted.

[
    {"lat":43.4627,"lng":-3.79636,"description":"a"}, 
    {"lat":43.4476,"lng":-3.82948,"description":"b"}, 
    {"lat":43.4228,"lng":-3.82391,"description":"c"}
]

1voto

Lucas D.A.W. Points 2104

Field names must be in double quotes instead of single quotes.

[
    {"lat":43.4627,"lng":-3.79636,"description":"a"}, 
    {"lat":43.4476,"lng":-3.82948,"description":"b"}, 
    {"lat":43.4228,"lng":-3.82391,"description":"c"}
]

json

According to the official documents :

A value can be a string between double quotation marks or a number, or true or false or null, or an object or an array .

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X