I have an output array like this after using "json.dumps(response.json(), indent=4"
{
"totalCount": 8,
"hasMore": false,
"firstIndex": 0,
"list": [
{
"id": "7d5bb8asdfasdfasfdasdfasdfasdf",
"name": "Corporate",
"domainType": "AAAAAAAA",
"description": "",
"createdBy": "admin",
"createDatetime": "2020/06/04 17:40:22",
"parentDomainId": "8b208asdfasdfasdfasdfasdfas",
"zoneCount": 2,
"subDomainCount": 1,
"administratorCount": 0,
"apCount": 0,
"zeroTouchStatus": true
},
Now when I try to filter it as follows
print(results['name']) or print(results['list'][0]['name'])
I keep getting this error message:
TypeError: string indices must be integers
This starts with a dict {}, then ther are lists [] of dict {} in here. Based on that it should work. Appreciate any guidance. Thank you.
It is clear by your data you posted that it is still in text format JSON. Convert to a python dict using json.loads()
result = json.loads(response.json())
print(results['name'])
print(results['list'][0]['name'])
The problem is json.dumps(response.json()). The return of this statement is a str so you cannot get items using [] notation. You can convert back it to a dict.
Related
I have the following code with a nested dictionary and I want to access the "assists" value for each participant and append it. How do I do that?
gameData = {
"metadata": {
"dataVersion": "2",
"matchId": "NA1_4263873346",
"participants": [
"Sw_wTB5fzxXvyXeCovgcGhKNw4hLKzgcEvWFKzMqZWtfiJ7HtbxYOK6Nb7nBU5SR-B3bNt4Ay9bvjQ",
"-t2_OfuyZFaCdZJ1lvbbfRFgYS1FWZcGhIsqj-8m-SS9UZ9wFyYeWBiGkcMgNEl_geH5CF9tX4SAzQ",
"A-n0X4QWr8Jr0PISogZK3VpnIqqVbm87jchMYpTrUrhiSfeoxVCl8ImnJxaE_lg9pIAdxNgaFpkT7g"
]
},
"info": {
"gameVersion": "12.6.431.7988",
"mapId": 11,
"participants": [
{
"assists": 9,
"deaths": 5,
"kills": 1
},
{
"assists": 1,
"deaths": 3,
"kills": 1
},
{
"assists": 3,
"deaths": 5,
"kills": 6
}
]
}
I use this to loop through them but it returns the error "TypeError: list indices must be integers or slices, not str".
participants = []
for row in gameData['metadata']['participants']:
participants_row = {}
participants_row['assists'] = gameData['info']['participants']['assists']
participants.append(participants_row)
Can anyone help me understand how to loop through it to get the desired values?
You have several mistakes in accessing parts of the dict.
I think you meant this:
# gameData = {"metadata": { ...
participants = []
for row in gameData['info']['participants']:
participants_row = {}
participants_row['assists'] = row['assists']
participants.append(participants_row)
print(participants)
Your original: for row in gameData['metadata']['participants']: is iterating over the wrong participants list. You meant: for row in gameData['info']['participants']:
participants_row['assists'] = match_detail['info']['participants']['assists']
I assume this line of code causes the error. To debug this, break it down into smaller pieces:
info = match_detail['info']
participants = info['participants']
assists = participants['assists']
If you do this, you will see that the third line causes the error. So add
print(participants)
Now we see that participants is a list, but you are using it like a dictionary.
I think you want the assists from each participant. One way to do this is with a list comprehension:
assists = [p['assists'] for p in participants]
Side note:
The python team is working on improving error messages so that the message points at the specific index that causes this kind of error. I don't know what version it will be released in, but I'm looking forward to it because this type of nested indexing is very common.
I'm new to Python and I'm trying to process something and having no luck finding the answer or if it's already been asked. I'm making a call to an API and receiving some data back as JSON. I'm stripping out certain bits that I don't need with the keys being stripped out and only the values remaining which wouldn't be a problem but I can't get into them as the keys I want to access are nested in an array.
I've been accessing the data and can get up to json.dumps(payload['output']['generic']) but I can't seem to find any information online as to how I can access these last values only.
Apologies in advance if this question already exists.
{
"output": {
"generic": [
{
"response_type": "text",
"text": "hi"
}
],
"intents": [
{
"intent": "CollectionDate",
"confidence": 0.8478035449981689
}
],
"entities": [
{
"entity": "Payslip",
"location": [
19,
26
],
"value": "When is my collection date",
"confidence": 1
}
]
},
"context": {
"global": {
"system": {
"turn_count": 10
}
},
"skills": {
"main skill": {
"user_defined": {
"DemoContext": "Hi!"
},
"system": {}
}
}
}
}
To clarify:
I want to access the "text", "intent" and "confidence"
at the moment I'm printing the value posted and then the responses for the sections I want like the below.
print(x)
print(json.dumps(payload['output']['generic']))
print(json.dumps(payload['output']['intents']))
Use following code to convert the json to a dict first:
json_data = json.loads(str(yourData))
After that, in your case, the outermost key is "output", and it is another dict, so just use json_data['output'] to access the content inside.
For other keys inside of the "output", like "generic", you can see it is an array with the [] brackets. use json_data['output'][index] first to get the content inside, then use the same method you access a dict to access the content inside of keys like this.
They key here is that the Traceback error indicates an issue with indexing a "List"
This is because a "List" type is a valid JSON type, and generic contains a list of length 1, with a dict inside!
>>> payload['output']['generic']['text']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
>>> type(payload['output']['generic'])
<class 'list'>
>>> len(payload['output']['generic'])
1
>>> payload['output']['generic'][0]
{'response_type': 'text', 'text': 'hi'}
>>> type(payload['output']['generic'][0])
<class 'dict'>
>>> payload['output']['generic'][0]['text']
'hi'
>>>
So, given your expected input JSON format, you will need to know how to index in to pull each required data point.
There are a few packages, glom is one, that will help you deal with missing values from API generated JSON.
I have a large list where each element of the list is a dictionary. In the dictionary there is nested another list which itself contains hundreds of key:value pairs. I would like to write this list to a file so that:
it is nicely formatted (human-readable). different "levels" of the file are differently indented
it is still a list (i.e. the file starts with [ and ends with ]
I'd like it to look something like this:
[{
"id": "1",
"day": 20190928,
"layer": {
"some_value": "value",
"some_other_value": 2,
"some_value_int": 5,
"imageFormat": "image/png",
},
"elements": [
{
"httpStatusCode": 200,
"requestTime": 1553731321446,
"some_attribute": 143,
"some_binary_value": True,
},
{
"httpStatusCode": 200,
"requestTime": 1553731321446,
"some_attribute": 143,
"some_binary_value": True,
},
# and so on...
I feel like it must be a trivial task but am a bit lost. This is what I have tried:
for item in converted_data:
for key, value in item.items():
if type(value) == dict:
#Implement
pass
elif type(value) == list:
#Implement
pass
else:
outfile.write(" {} : {},\n".format(key, value))
But even before finishing it I see it is a wrong approach that would make something this simple very complicated. I have looked at SO but didn't find questions similar to my problem. So, how should I do this?
I don't need to be the file to be exactly like I suggested. I just need it to be both human- and machine-readable.
I will be grateful for any suggestions as to how to solve this.
import json
list_of_dicts # your list
data = json.dumps(list_of_dicts, indent = 4)
# write data to a file...
This will use 4 spaces for indentation, change 4 to something else if you want.
I do this by using Idents[0].IsVerified but I do not want to give index number. I need to check which block is associate with PrimaryEmail or Mobile. This is my JSON:
"Idents": [
{
"PrimaryEmail": "abc#gmail.com",
"IsVerified": false,
"IdentId": 1,
"EmailVerificationCode": 302284
},
{
"Mobile": "1234567890",
"IsVerified": true,
"IdentId": 2,
"MobileVerificationCode": 302284
},
{
"CardNumber": 0,
"IsVerified": false,
"IdentId": 4
}
]
Python does support JSON directly. First, you must convert JSON into lists and dictionaries (module json has the necessary tools). Assuming you've done the conversion and the JSON array is in the list Idents, look through the list and check which list items have the key "Mobile":
[block for block in Idents if "Mobile" in block]
#[{'MobileVerificationCode': 302284, 'IdentId': 2, 'IsVerified': True,
# 'Mobile': '1234567890'}]
The same result can be obtained by filtering:
list(filter(lambda block: "Mobile" in block, Idents))
#[{'MobileVerificationCode': 302284, 'IdentId': 2, 'IsVerified': True,
# 'Mobile': '1234567890'}]
I have a some variables that I need to dump as a json of the format:
{
"elements":[
{
"key":"foo",
"value":"7837"
},
{
"key":"bar",
"value":"3423"
}
]
}
I am trying to figure out the right object which would give the above structure upon usin json.dumps(). I see that in python, lists give a json array where as dictionaries give a json object while using json dumps.
I am trying something like:
x={}
x["elements"]={}
x["elements"]["key"]="foo"
x["elements"]["value"]="7837"
x["elements"]["key"]="bar"
x["elements"]["value"]="3423"
json_x=json.dumps(x)
But this still gives me:
{"elements": {"key": "bar", "value": "3423"}}
which is obviously incorrect.
How to I incorporate the correct dictionary and list structure to get to the above json?
Why don't you just use literal?
x = {
"elements": [
{"key":"foo", "value":"7837"},
{"key":"bar", "value":"3423"}
]
}
To fix your code, you need to use a list literal ([]) when assigning to elements dictionary entry:
>>> x = {}
>>> x["elements"] = [] # <---
>>> x["elements"].append({})
>>> x["elements"].append({})
>>> x["elements"][0]["key"]="foo"
>>> x["elements"][0]["value"]="7837"
>>> x["elements"][1]["key"]="bar"
>>> x["elements"][1]["value"]="3423"
>>> json.dumps(x)
'{"elements": [{"value": "7837", "key": "foo"}, {"value": "3423", "key": "bar"}]}'
But, it's hard to read, maintain.