Sandeep Oberoi
Sandeep Oberoi

Reputation: 45

Not able to Parse a Json File

I want to load a GeoJson File that is uploaded on server

var promise = ('https://api.myjson.com/bins/31e3j');
that.map.data.loadGeoJson(promise);

This condition works fine

But I want to Load this GeoJson File locally so I have assigned the Json Code instead a the Server Link to a Variable on which I am neither getting any error but unable to get the O/P as well

var promise = jQuery.parseJSON ('{ "type": "FeatureCollection","crs":{"type": "name","properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84"}},"features": [{"type": "Feature", "properties": {"id": 1},"geometry": {"type": "Polygon", "coordinates": [ [  [  -83.52936044652942, 40.30230752849768], [ -83.52924865349425,  40.30230753872012], [ -83.52924666169983, 40.3021800251207 ], [ -83.52935848418728, 40.302181900418084 ], [ -83.52936044652942, 40.30230752849768]]]}}, ]}');
that.map.data.loadGeoJson(promise);

Upvotes: 1

Views: 504

Answers (3)

Adrian Lynch
Adrian Lynch

Reputation: 8494

When in doubt, run it through a linter/formatter:

http://jsonlint.com/

You have an error in the JSON, a comma a few characters from the end:

]]]}}, ]}');
     ^-------TROUBLE MAKER!

Or this one is cool!

http://pro.jsonlint.com/

I am neither getting any error

Maybe the surrounding code is swallowing the error. If you take your var promise = jQuery.parseJSON('DODGY_JSON_HERE') code and run it in the console, you'll see the error:

Uncaught SyntaxError: Unexpected token ](…)
   e.extend.parseJSON              @jquery.min.js:2
   (anonymous function)            @VM270:2
   InjectedScript._evaluateOn      @VM268:875
   InjectedScript._evaluateAndWrap @VM268:808
   InjectedScript.evaluate         @VM268:664

Not as handy as the linter, but at least you see an error.

Upvotes: 5

dkocich
dkocich

Reputation: 221

Because that isn't correct JSON. You have additional comma at the end.

{ "type": "FeatureCollection","crs":{"type": "name","properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84"}},"features": [{"type": "Feature", "properties": {"id": 1},"geometry": {"type": "Polygon", "coordinates": [ [ [ -83.52936044652942, 40.30230752849768], [ -83.52924865349425, 40.30230753872012], [ -83.52924666169983, 40.3021800251207 ], [ -83.52935848418728, 40.302181900418084 ], [ -83.52936044652942, 40.30230752849768]]]}} ]}

This is correct JSON:

{
    "type": "FeatureCollection",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "features": [
        {
            "type": "Feature",
            "properties": {
                "id": 1
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            -83.52936044652942,
                            40.30230752849768
                        ],
                        [
                            -83.52924865349425,
                            40.30230753872012
                        ],
                        [
                            -83.52924666169983,
                            40.3021800251207
                        ],
                        [
                            -83.52935848418728,
                            40.302181900418084
                        ],
                        [
                            -83.52936044652942,
                            40.30230752849768
                        ]
                    ]
                ]
            }
        }
    ]
}

Upvotes: 1

Marc B
Marc B

Reputation: 360572

Invalid JSON is not parseable, obviously:

...snip...[ -83.52936044652942, 40.30230752849768]]]}}, ]}');
                                                       ^----

Upvotes: 2

Related Questions