Reputation: 1
My code is as follows: The JSON File is https://www.dropbox.com/s/uwqfqb27blxr1bj/citiesclimate_2014_03_27.json
A small sample:
[ { "_id" : { "$oid" : "5333d7e18828169279d9250d" },
"actions" : null,
"actions_nr" : 0,
"city" : "Adachi City",
"citylink" : "<a href=\"/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198\" target=\"_blank\" >Adachi City</a>",
"commitments" : "338,339",
"commitments_nr" : 1,
"country" : "Japan",
"latitude" : "35.465",
"longitude" : "139.482",
"performance" : "355,356,1090,1091",
"performance_nr" : 4,
"uid" : "198"
},
{ "_id" : { "$oid" : "5333d7e18828169279d92511" },
"test" : [ { "actions" : null,
"actions_nr" : 0,
"city" : "Adachi City",
"citylink" : "<a href=\"/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198\" target=\"_blank\" >Adachi City</a>",
"commitments" : "338,339",
"commitments_nr" : 1,
"country" : "Japan",
"latitude" : "35.465",
"longitude" : "139.482",
"performance" : "355,356,1090,1091",
"performance_nr" : 4,
"uid" : "198"
},
{ "actions" : "3025,3105,3106,3108,3109,3110,3111,3112,3113,3114,3115,3116,3164,3166,3167,3168,3170,3171,3172,3173,3174,3175,3176,3177,3180,3181,3182,3183,3184,3185,3187,3188,3189,3190,3191,3192,3193,3194,3196,3197,3410",
"actions_nr" : 41,
"city" : "Ahmadabad City",
"citylink" : "<a href=\"/data/report/commitments/?tx_datareport_pi1%5Buid%5D=549\" target=\"_blank\" >Ahmadabad City</a>",
"commitments" : "816",
"commitments_nr" : 1,
"country" : "India",
"latitude" : "23.0300",
"longitude" : "72.5800",
"performance" : "900,901",
"performance_nr" : 2,
"uid" : "549"
}
]
}
]
I keep getting string indices must be integers
json_file = source_json
with open(json_file) as json_file:
json_data = json.load(json_file)
for e in json_data: # iterator over a dictionary
#print e
for key, value in e.iteritems():
if key != '_id':
print key, value
#city_climate_data['city'] = value['test.city']
#print city_climate_data['city']
Upvotes: 0
Views: 17975
Reputation:
I keep getting string indices must be integers
In JSON a pair must be string : value
. It is not possible to have an integer as key
.
Edit
I've added a small sample of the JSON to your question. With this JSON, your code will not work because a dictionary does not have iteritems
. You can do this:
for e in json_data:
for k, v in e.items():
if k != '_id':
print k, v
That would give this output:
country, Japan
citylink, <a href="/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198" target="_blank" >Adachi City</a>
commitments, 338,339
commitments_nr, 1
longitude, 139.482
performance_nr, 4
actions_nr, 0
latitude, 35.465
uid, 198
actions, None
performance, 355,356,1090,1091
city, Adachi City
test, [{'country': 'Japan', 'commitments': '338,339', 'citylink': '<a href="/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198" target="_blank" >Adachi City</a>', 'commitments_nr': 1, 'longitude': '139.482', 'performance_nr': 4, 'actions_nr': 0, 'latitude': '35.465', 'uid': '198', 'actions': None, 'performance': '355,356,1090,1091', 'city': 'Adachi City'}, {'country': 'India', 'commitments': '816', 'citylink': '<a href="/data/report/commitments/?tx_datareport_pi1%5Buid%5D=549" target="_blank" >Ahmadabad City</a>', 'commitments_nr': 1, 'longitude': '72.5800', 'performance_nr': 2, 'actions_nr': 41, 'latitude': '23.0300', 'uid': '549', 'actions': '3025,3105,3106,3108,3109,3110,3111,3112,3113,3114,3115,3116,3164,3166,3167,3168,3170,3171,3172,3173,3174,3175,3176,3177,3180,3181,3182,3183,3184,3185,3187,3188,3189,3190,3191,3192,3193,3194,3196,3197,3410', 'performance': '900,901', 'city': 'Ahmadabad City'}]
What part of the JSON are you interested in?
Upvotes: 1