geoJson : Json.load() ValueError: Expecting property name: line 2 column 9

时间:2016-10-20 12:59:13

标签: python json django geojson

I have trouble loading my json file using the json.load(). My format is a geojson with the following example format:

    { "type" : "Feature Collection",
      {"features" : [
            { "type" : "Feature",
        "id" : "FORT_1",
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["121.046859", "14.54544278"]},
        "properties" : { "name" : "City"}
         },
            { "type" : "Feature",
        "id" : "FORT_2",
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["121.0500991", "14.54973692"]},
        "properties" : { "name" : "One"}
         },

I wanted to test it out using the following code, knowing that this will create a dict:

            with open(geojsonFilePath) as file:
            jsonFile = json.load(file)

            for feature in jsonFile['features']:
                print (['id'])
                print (['geometry'],['type'])
                print (['geometry'],['coordinates'])
                print (['properties'],['name'])

However the error occurs at the line jsonFile = json.load(file)

1 个答案:

答案 0 :(得分:0)

Here are all the issues with your JSON I could find

Firstly, at

{ "type" : "Feature Collection",
   {"features" : [

You're adding an object without a key, it should be either

{ "type" : "Feature Collection",
  "features" : [

or

{ "type" : "Feature Collection",
  "somekey": features" : [

Then, there's FORT_1 and FORT_2, which look like they are variable names or constants, so this should be either enclosed in quotes, or the values of those variables/constant needs to be substituted.

Finally, there are many mismatched braces, not sure if you have shared a snippet or the whole json, but the array brace of features is not closed and subsequent braces aren't closed.

I've corrected your input making a few assumptions for reference.

 {
    "type": "Feature Collection",

    "features": [{
        "type": "Feature",
        "id": "FORT_1",
        "geometry": {
            "type": "Point",
            "coordinates": ["121.046859", "14.54544278"]
        },
        "properties": {
            "name": "City"
        }
    }, {
        "type": "Feature",
        "id": "FORT_2",
        "geometry": {
            "type": "Point",
            "coordinates": ["121.0500991", "14.54973692"]
        },
        "properties": {
            "name": "One"
        }
    }]

 }