dlofrodloh
dlofrodloh

Reputation: 1744

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?

Upvotes: 0

Views: 170

Answers (3)

Antony Pegg
Antony Pegg

Reputation: 26

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.

Upvotes: 0

Matt Gerrans
Matt Gerrans

Reputation: 71

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

Upvotes: 1

Clint
Clint

Reputation: 2793

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"
            }]
        }]
    }
}

Upvotes: 0

Related Questions