Unexpected token { in JSON

765 Views Asked by At

hello i am working with Elastic Search and Golang and after fetching data from elastic search index in Golang when i send the data to jquery using Json Encoder function of Golang i get "Unexpected token { in JSON " error while parsing the data in jquery

this is what Goland Json Encoder sends to Jquery:

{"id":212,"user_id":10,"meta_description":"Plot,G-16, Islamabad,In G-16, Islamabad, Islamabad","property_type":"16","Location1":"Pakistan","Location2":"Islamabad","Location3":null,"Location4":"Islamabad","price":1850000,"bedrooms":0,"bathrooms":0,"add_date":{"Time":"0001-01-01T00:00:00Z","Valid":false}}
{"id":213,"user_id":10,"meta_description":"Plot,G-16, Islamabad,In G-16, Islamabad, Islamabad","property_type":"16","Location1":"Pakistan","Location2":"Islamabad","Location3":null,"Location4":"Islamabad","price":1800000,"bedrooms":0,"bathrooms":0,"add_date":{"Time":"0001-01-01T00:00:00Z","Valid":false}}
2

There are 2 best solutions below

2
On BEST ANSWER

Your JSON data is missing two Important things, One is, encapsulate it in square brackets and a ',' for each data as separator. More or less it should look like this:

[{
    "id": 212,
    "user_id": 10,
    "meta_description": "Plot,G-16, Islamabad,In G-16, Islamabad, Islamabad",
    "property_type": "16",
    "Location1": "Pakistan",
    "Location2": "Islamabad",
    "Location3": null,
    "Location4": "Islamabad",
    "price": 1850000,
    "bedrooms": 0,
    "bathrooms": 0,
    "add_date": {
        "Time": "0001-01-01T00:00:00Z",
        "Valid": false
    }
}, {
    "id": 213,
    "user_id": 10,
    "meta_description": "Plot,G-16, Islamabad,In G-16, Islamabad, Islamabad",
    "property_type": "16",
    "Location1": "Pakistan",
    "Location2": "Islamabad",
    "Location3": null,
    "Location4": "Islamabad",
    "price": 1800000,
    "bedrooms": 0,
    "bathrooms": 0,
    "add_date": {
        "Time": "0001-01-01T00:00:00Z",
        "Valid": false
    }
}]

I have made very small change, If you are not sure about the data the best option would be to try to validate it here : https://jsonlint.com/

2
On

Looks like you're trying to send 2 separate JSONs without any sort of connector. Try wrapping the whole thing in a { }, and putting a comma before {"id": 213.