0

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?

JAAulde
  • 19,250
  • 5
  • 52
  • 63
dlofrodloh
  • 1,728
  • 3
  • 23
  • 44
  • http://stackoverflow.com/questions/2887209/what-are-the-differences-between-json-and-jsonp – michaJlS May 20 '16 at 23:30
  • 1
    You are getting back JSONP as opposed to JSON. Read through the link posted in the comment above mine, and look through Mapquest's documentation on how to get back JSON instead. – JAAulde May 20 '16 at 23:35

3 Answers3

1

Remove the renderOptions() and what's inside those parentheses is JSON, starting with the first curly and ending with the last curly.

Matt Gerrans
  • 61
  • 1
  • 6
0

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"
            }]
        }]
    }
}
Clint
  • 2,696
  • 23
  • 42
0

in your URL to the mapquest geocoding service, you will have a parameter "callback=renderOptions" - probably copied & pasted from an example. Remove that parameter to remove the callback wrapping.