Hello I would like to get all the 'Integer' values from a dict:
array_test = [{ "result1" : "date1", "type" : "Integer"},{ "result1" : "date2", "type" : "null"}]
I tried:
test = {'result1':array_test['result1'] for element in array_test if array_test['type'] == "Integer"}
However I got this error:
>>> test = {'result1':array_test['result1'] for element in array_test if array_test['type'] == "Integer"}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <dictcomp>
TypeError: list indices must be integers or slices, not str
>>>
>>>
So I would like to appreciate support to achieve the following output
test = [{ "result1" : "date1", "type" : "Integer"}]
You need a list-comprehension, not dictionary-comprehension:
array_test = [{ "result1" : "date1", "type" : "Integer"},{ "result1" : "date2", "type" : "null"}]
test = [x for x in array_test if x['type'] == 'Integer']
# [{'result1': 'date1', 'type': 'Integer'}]
Why? Because required output is a list (list of dictionaries).
Related
I have this code below which aims to unpack a nested record when found. Sometimes it works and sometimes it throws an error.
Would anyone have an idea how to resolve this?
Data (Works):
d = {
"_id" : 245,
"connId" : "3r34b32",
"roomList" : [
{
"reportId" : 29,
"siteId" : 1
}]
}
Data (Doesn't work):
d = {
"_id" : 2,
"connId" : 128,
"Config" : {
"Id" : 5203,
"TemplateId" : "587",
"alertRules" : [
{
"id" : 6,
"daysOfTheWeek" : [
"mon",
"tue",
"wed",
"thu",
"fri",
"sat",
"sun"
],
}
]
}}
Code (Dynamic):
root = pd.json_normalize(d)
nested_cols = [i for i in root.columns if isinstance(root[i][0], list)]
l = [root.drop(nested_cols,1),]
for i in nested_cols:
l.append(pd.json_normalize(d, record_path=i))
output = pd.concat(l, axis=1)
print(output)
Traceback Error:
Traceback (most recent call last):
File "c:/Users/Max/Desktop/Azure/TestTimerTrigger/testing.py", line 30, in <module>
l.append(pd.json_normalize(d, record_path=i))
File "c:\Users\Max\Desktop\Azure\.venv\lib\site-packages\pandas\io\json\_normalize.py", line 336, in _json_normalize
_recursive_extract(data, record_path, {}, level=0)
File "c:\Users\Max\Desktop\Azure\.venv\lib\site-packages\pandas\io\json\_normalize.py", line 309, in _recursive_extract
recs = _pull_records(obj, path[0])
File "c:\Users\Max\Desktop\Azure\.venv\lib\site-packages\pandas\io\json\_normalize.py", line 248, in _pull_records
result = _pull_field(js, spec)
File "c:\Users\Max\Desktop\Azure\.venv\lib\site-packages\pandas\io\json\_normalize.py", line 239, in _pull_field
result = result[spec]
KeyError: 'Config.alertRules'
Expected Output:
_id,connid,config.id,config.templateid,id,daysoftheweek
2,128,5203,587,6,[mon,tue,wed,thu,fri,sat,sun]
Note:
I know a keyerror is when the key in a dictionary cannot be located, however, I'm unsure how to go about resolving this.
Any help or guidance will be greatly appreciated.
It is looking for the key Config.alertRules in your dict like d["Config.alertRules"]. It is a nested dict so you should index it like d["Config"]["alertRules"], how are you passing these keys?
This error probably does not occur for your first dictionary since there are no nested dicts there. (d["roomList"] is a list)
I have below dictionary in python
>>> dict = {"name":"myname", "class": "10", "score %": "60"}
I have converted from dictionary to json format like below
>>> json_format = json.dumps(dict)
>>> print json_format
{"score %": "60", "name": "myname", "class": "10"}
I am trying to get value of key from json_format variable:
>>> print json_format["name"]
I am getting below error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers, not str
Please help me where i am doing mistake.
json.dumps() returns the string representation so you can't access it like a json datatype.
You need to do something like this:
import json
dict = {"name":"myname", "class": "10", "score %": "60"}
json_format = json.dumps(dict)
If you check the json_format type():
print(type(json_format))
Output:
<class 'str'>
So you need to do this:
json_format = json.loads(json_format)
print(json_format["name"])
I am getting the error message : Traceback (most recent call last):
File "", line 10, in
TypeError: string indices must be integers . please help me.
the python 2.7 script sample:
import csv
cursor = db.job_templates.find( {}, {'_id': 1, 'rangePercents.questionnaire': 1, 'rangePercents.termMap': 1})
with open('range_percent1.csv', 'wt') as outfile:
fields = ['_id', 'questionnaire', 'termMap']
write = csv.DictWriter(outfile, fieldnames=fields)
write.writeheader()
for x in cursor:
x_id = x['_id']
for y in x['rangePercents']:
z = {
'_id': x_id,
'rangePercents.questionnaire': y['questionnaire'],
'rangePercents.termMap': y['termMap']}
write.writerow(z)
the data sample is
"_id": ObjectID("51dc52fec0d988a9547b5201"),
"rangePercents": {
"termMap": 0,
"questionnaire": 100
}
Don't iterate over x['rangePercents'], you already can access everything in it through x. If you iterate like you're doing, you just get each key of your dictionary as y, which is a string and thus why you get your error. Try this:
for x in cursor:
x_id = x['_id']
y = x['rangePercents']
z = {
'_id': x_id,
'rangePercents.questionnaire': y['questionnaire'],
'rangePercents.termMap': y['termMap']}
write.writerow(z)
After submitting a request I have received the following json back:
{"type": [
{"ID": "all", "count": 1, "references": [
{ "id": "Boston,MA,02118", "text": "Boston,MA,02118", "val": "Boston,MA,02118", "type": 1 ,"zip": "02118","city": "Boston","state": "MA","lt": "42.3369","lg": "-71.0637","s": ""}
] }
] }
I captured the response in variable j and loaded it as follows,
l = json.loads(j)
Now I have:
>>> type(l)
<type 'dict'>
>>> l['type']['references']
Traceback (most recent call last):
File "C:\PyCharm\helpers\pydev\pydevd_exec.py", line 3, in Exec
exec exp in global_vars, local_vars
File "<input>", line 1, in <module>
TypeError: list indices must be integers, not str
What am I doing wrong?
l['type'] gives you a list, not an object. So you have to access it like a list
l['type'][0]["references"]
The value of the key type is a list. Try doing type(l['type']) to confirm.
To access that value you would need to use:
l['type'][0]['references']
which would give you another list.
I am trying to Parse a Json array, A sample of the array i get is below with my code.
I can not seem to workout what my issue is, please forgive my question if I have included too much
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
map = "[{'network' : 'networkA','ycoord' : '73','zcoord' : '-2612','xcoord' : '-4461','owner' : 'PlayerA','name' : 'PlaceA'}, {'network' : 'NetworkB','ycoord' : '66','zcoord' : '-1915','xcoord' : '1156','owner' : 'PlayerB','name' : 'PlaceB'}, {'network' : 'NetWorkB','ycoord' : '71','zcoord' : '3091','xcoord' : '4541','owner' : 'PlayerB','name' : 'PlaceC'}, {'network' : 'NetworkB','ycoord' : '118','zcoord' : '-66','xcoord' : '5','owner' : 'PlayerB','name' : 'PlaceD'}, {'network' : 'networkA','ycoord' : '71','zcoord' : '761','xcoord' : '-248','owner' : 'PlayerA','name' : 'PlaceE'}]"
data = json.load(map)
for item in data:
print "Network : "+ str(item['network'])
print "Name : "+ str(item['name'])
print "Owner : "+ str(item['owner'])
print "Co ords : ("+ str(item['ycoord']+", "+ str(item['xcoord']+", "+ str(item['Zcoord']+")"
I get The error
File "test.py", line 8, in <module>
data = json.load(map)
File "/usr/lib/python2.7/json/__init__.py", line 274, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
Readable Json Data (because I know what's in the code isn't)
[{
'network' : 'networkA',
'ycoord' : '73',
'zcoord' : '-2612',
'xcoord' : '-4461',
'owner' : 'PlayerA',
'name' : 'PlaceA'
}, {
'network' : 'NetworkB',
'ycoord' : '66',
'zcoord' : '-1915',
'xcoord' : '1156',
'owner' : 'PlayerB',
'name' : 'PlaceB'
}, {
'network' : 'NetWorkB',
'ycoord' : '71',
'zcoord' : '3091',
'xcoord' : '4541',
'owner' : 'PlayerB',
'name' : 'PlaceC'
}, {
'network' : 'NetworkB',
'ycoord' : '118',
'zcoord' : '-66',
'xcoord' : '5',
'owner' : 'PlayerB',
'name' : 'PlaceD'
}, {
'network' : 'networkA',
'ycoord' : '71',
'zcoord' : '761',
'xcoord' : '-248',
'owner' : 'PlayerA',
'name' : 'PlaceE'
}]
You want loads() instead of load(). Read the documentation, load() takes a filename, loads() takes actual JSON data.
json.load() function will require filename as a parameter. In your case, You don't want filename but an actual JSON array.
Use json.loads() instead of json.load()
Also, remember The functions with an s take string parameters. The others take file
streams. This applies to json.dump() and json.dumps() too.