dlofrodloh dlofrodloh - 6 months ago 26
JSON Question

Issue with JSON syntax

I'm not to familar with JSON, I mainly do PHP. When I run a query to the Mapquest geocoding API, I get the following json string which becomes "NULL" when attempting to json_decode it in PHP.

renderOptions({
"info": {
"statuscode": 0,
"copyright": {
"text": "\u00A9 2016 MapQuest, Inc.",
"imageUrl": "https://api.mqcdn.com/res/mqlogo.gif",
"imageAltText": "\u00A9 2016 MapQuest, Inc."
},
"messages": []
},
"options": {
"maxResults": -1,
"thumbMaps": true,
"ignoreLatLngInput": false
},
"results": [{
"providedLocation": {
"street": "Kingston Upon Thames,uk"
},
"locations": [{
"street": "",
"unknownInput": "",
"type": "s",
"latLng": {
"lat": 51.409628,
"lng": -0.306262
},
"displayLatLng": {
"lat": 51.409628,
"lng": -0.306262
},
"mapUrl": "https://open.mapquestapi.com/staticmap/v4/getmap?key=na&type=map&size=225,160&pois=purple-1,51.4096275,-0.3062621,0,0,|¢er=51.4096275,-0.3062621&zoom=12&rand=54353"
}]
}]
})


Running it through JSONLint, I get the following error:


Error: Parse error on line 1: renderOptions({ "in ^ Expecting
'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'


I'd imagine the fix is quite simple, but I'm not too aware of JSON syntax so I've been fumbling around with putting stuff before 'renderOptions'.

What would be the correct syntax to fix the issue?

Answer

This would be a valid json

{
    "renderOptions": {
        "info": {
            "statuscode": 0,
            "copyright": {
                "text": "\u00A9 2016 MapQuest, Inc.",
                "imageUrl": "https://api.mqcdn.com/res/mqlogo.gif",
                "imageAltText": "\u00A9 2016 MapQuest, Inc."
            },
            "messages": []
        },
        "options": {
            "maxResults": -1,
            "thumbMaps": true,
            "ignoreLatLngInput": false
        },
        "results": [{
            "providedLocation": {
                "street": "Kingston Upon Thames,uk"
            },
            "locations": [{
                "street": "",
                "unknownInput": "",
                "type": "s",
                "latLng": {
                    "lat": 51.409628,
                    "lng": -0.306262
                },
                "displayLatLng": {
                    "lat": 51.409628,
                    "lng": -0.306262
                },
                "mapUrl": "https://open.mapquestapi.com/staticmap/v4/getmap?key=na&type=map&size=225,160&pois=purple-1,51.4096275,-0.3062621,0,0,|¢er=51.4096275,-0.3062621&zoom=12&rand=54353"
            }]
        }]
    }
}