0

My geoJSON looks like so

{
"type": "FeatureCollection",
"crs": {
    "type": "name",
    "properties": {
        "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
},

"features": [{
        "type": "Feature",
        "properties": {
            "value1": "abc",
            "value2": 0,
            "value3": 0.99,
            "value4": "def",
            "value5": "882.3",
            "value6": 12,
        },
        "geometry": {
            "type": "Point",
            "coordinates": [1, 1]
        }
    }
]
}

I want to access properties and check some values for a key

for features in geoJsonPoints["features"]:
    for interesting in features["properties"]["value1"]:
        print interesting
        print "!"

I get

a

!

b

!

c

!

Why is that?! It seems like my loop does not return me a dictionary?!

If I do this

for features in geoJsonPoints["features"]:
    for interesting in features["properties"]:
        print type(intereseting)
        print interesting

I get

type 'unicode'

value1

type 'unicode'

value2

...

Why isnt that a dictionary? And, if its not a dictionary, why can I access the values behind the "unicode" like in the first loop I showed?!

Community
  • 1
  • 1
four-eyes
  • 10,740
  • 29
  • 111
  • 220

1 Answers1

2

features["properties"]["value1"] points to abc string which you iterate over character by character. Instead, you probably meant to iterate over the properties dictionary:

for property_name, property_value in features["properties"].items():
    print(property_name, property_value)

Or, you can loop over the dictionary keys:

for property_name in features["properties"]:
    print(property_name, features["properties"][property_name])

See more about dictionaries and looping techniques here:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • why do I need the `item()` for that? – four-eyes Apr 20 '16 at 22:18
  • @Stophface `items()` (as well as `iteritems()` in Python 2) gives you access to both keys and values at the same time. Please follow the posted links for more information. Thanks. – alecxe Apr 20 '16 at 22:21
  • Hm, I thought when I can iterate like in my first loop through a dictionary, why can't i do it in my nested loop then? – four-eyes Apr 20 '16 at 22:22
  • @Stophface `features["properties"]` in your case is a dictionary. If you iterate over it as `for interesting in features["properties"]`, you will get the keys only (well, you would be able to access values as `features["properties"][interesting]` of course). But `items()` allows you to access both keys and values at the same time. Sorry, I'm quite terrible at explaining and teaching :) – alecxe Apr 20 '16 at 22:23
  • "If you iterate over it as for interesting in features["properties"], you will get the keys only (well, you would be able to access values as features["properties"][interesting] of course)." Well thats problematic because it will, as my thread showed, return letter by letter?! – four-eyes Apr 20 '16 at 22:31
  • @Stophface nope, try it out, in this case `interesting` should contain your dictionary keys (`value1`, `value2` etc) and `features["properties"][interesting]` would contain dictionary values: (`abc`, `0` etc).. – alecxe Apr 20 '16 at 22:32
  • nope. When I write `[interesting]` without quotationmarks it says `its not defined`. I thought so too (as i.e. here http://www.jroller.com/evans/entry/parsing_json_with_python). Have a look at my question. I basically wrote it the way you just suggested (except the quotationmarks). But I think the problem is that `properties` in my JSON is not written like so `[{...]}` but like so `{}`. Try it out, it wont work. – four-eyes Apr 20 '16 at 22:35
  • @Stophface okay, could you try the second code snippet I've posted? The one after `Or, you can loop over the dictionary keys`. Thanks. – alecxe Apr 20 '16 at 22:37
  • That does not work. It says `property_name` (which i replaced with `value1`) not defined. `print property_name` works! I am on python 2.7 btw. – four-eyes Apr 20 '16 at 22:41