From the JSON string below I'm trying to pull just all the waiverId's:
data =
{
"version": 4,
"id": "(requestId)",
"ts": "2022-11-14T20:24:50 00:00",
"type": "checkins",
"checkins": {
"fromDts": "2022-07-01",
"toDts": "2022-07-02",
"moreCheckins": true,
"checkins": [
{
"date": "2022-07-01 15:18:09",
"waiverId": "(id1)",
"position": 0,
"firstName": "(first name)",
"lastName": "(last name)"
},
{
"date": "2022-07-01 15:19:10",
"waiverId": "(id2)",
"position": 0,
"firstName": "(first name)",
"lastName": "(last name)"
}
]
}
}
I have tried the following:
for checkins in data['checkins']:
print(checkins)
Which just gives:
fromDts
toDts
moreCheckins
checkins
I would like just a list of: id1 id2
CodePudding user response:
You want to pull the list of checkins
from 'checkins'
as in:
for checkins in data['checkins']['checkins']:
print(checkins)
The first key checkins
returns a dict
value containing a key of checkins
that contains a list of the data you want in dict
format.
CodePudding user response:
Assumptions:
- The dictionary you deal with is already in the form that can be processed by python (so 'true' becomes 'True').
response.json()
for this answer is not necessary, you should better specify what you want to do. I will treat the given variable directly as the dictionary in the correct form.
Simply iterate through the dataframe as indicated by @user99999 and add the elements you are looking for to the list:
data = {
"version": 4,
"id": "(requestId)",
"ts": "2022-11-14T20:24:50 00:00",
"type": "checkins",
"checkins": {
"fromDts": "2022-07-01",
"toDts": "2022-07-02",
"moreCheckins": True,
"checkins": [
{
"date": "2022-07-01 15:18:09",
"waiverId": "(id1)",
"position": 0,
"firstName": "(first name)",
"lastName": "(last name)"
},
{
"date": "2022-07-01 15:19:10",
"waiverId": "(id2)",
"position": 0,
"firstName": "(first name)",
"lastName": "(last name)"
}
]
}
}
waiverIds = []
for checkins in data['checkins']['checkins']:
waiverIds.append(checkins['waiverId'])
print(waiverIds)
Output will be:
['(id1)', '(id2)']