How can I get the Venue ID from the response - python

Since I just started with Python, I have some difficulties with accessing the keys.
I want to loop through each Foursquare venue I got as an JSON response, to get the Venue ID. The Venue ID then should be added as a parameter to get all the details to the venues.
I already tried it with a for loop, but it did not work:
for a in data['response']['groups']['items']['venue']:
my_place_id = place['id']
venue_ID_resp = requests.get(url=url_venue_details,params=my_place_id)
Error messsage:
for a in data['response']['groups']['items']['venue']:
TypeError: list indices must be integers or slices, not str
The response looks like this:
'response':{
'groups':[
{
'items':[
{
'reasons':{
'count':0,
'items':[
{
'reasonName':'globalInteractionReason',
'summary':'This ' 'spot ' 'is ' 'popular',
'type':'general'
}
]
},
'referralId':'e-0-52bf5eca498e01893b7004fb-0',
'venue':{
'categories':[
{
'icon':{
'prefix':'https://ss3.4sqi.net/img/categories_v2/travel/hostel_',
'suffix':'.png'
},
'id':'4bf58dd8d48988d1ee931735',
'name':'Hostel',
'pluralName':'Hostels',
'primary':True,
'shortName':'Hostel'
}
],
'id':'52bf5eca498e01893b7004fb',
'location':{
I only want to get the Venue ID, like:
print(my_place_id)
4bf58dd8d48988d1ee931234
4bf58dd8d48988d1ee945225
4bf58dd8d48988d1ee931888
4bf58dd8d48988d1ee564563
.
.
.

Your Json contains lists. You can get id:
my_id = data['response']['groups']['items'][0]['venue']['categories'][0]['id']
Note: If you have more elements in your lists, you can create for loop and get all ID.

items inside group in your response JSON is a list. This is the reason you get the error: " TypeError: list indices must be integers or slices, not str" as list index cannot be a string.
As correctly pointed out by #tfw, even groups is a list, so it should be
You need to access it as data['response']['groups'][0]['items'][0]['venue']
If there are going to multiple elements in the items list, then better to loop through them and access like below
for x in data['response']['groups']:
for y in x['items']
print y['venue']

Related

How to extract a key pair value from a JSON array?

I'm using a GET query string to locate a specific record. The correct result is returned in JSON, but in an array of one (it allows for multiple items, but as I'm using a unique key in my query string, there will only ever be one item returned, but's it's still in an array). I've been able to extract the value of a specific key pair from a JSON result before, but this time the fact that this particular result is in an array has got me stumped. I'm a set of square brackets away from success :-(
Here's the JSON response, and I want the value of 'Id':
{
'#odata.context': 'https://foo.bar.com/mySite/api/v2/object/$metadata#MuseuSensorData_SensorDeviceObject',
'value':
[
{
'Id': '02cb9c0f-89ca-46fd-882e-5d770d7da214',
'DateCreated': '2022-07-13T14:05:22.24+01:00',
'DeviceName': 'Cabinet 2 Sensor 1',
'IsActive': True,
'SerialNumber': '1234'
}
]}
In amongst my Python code, extract below, I'm trying to return the value of 'Id', but all I get is any one of a number of errors (latest is TypeError: string indices must be integers), which I know relates to indices/dictionaries and something called slices, and navigating through to the desired section, but I'm new to Python (as of this week), and it's got me spinning.
response = requests.request("GET", url, headers=headers)
data = response.text
print(data[0]['value']['Id'])
Looks like I was almost there. Thanks to Kacper, I now have my code in the right order:
data = response.text
parsed_json = json.loads(data)
RecID = parsed_json['value'][0]['Id']
print(RecID)

Pymongo get random record and element of the record

I have a record like this in my MongoDB
{
_id: "611ae422a01534cecce5533d"
firstname:"Test1"
lastname: "Test2"
}
This is my code
res = firstnameCollection.aggregate([{'$sample': {'size': 1 }}])
print(list(res))
I want to get a random record out of my database. I have many records like I mentioned above.
The output I get is a list of the record. When I remove the list attribute, I get:
<pymongo.command_cursor.CommandCursor object at 0x0000021078B82EB0>
I just want to output the firstname of the element:
Test1
Once you convert the response to a list, you can retrieve the first element using an index. You could do something like this:
docs = list(res)
first_name = docs[0]['firstname'] if docs and 'firstname' in docs[0] else 'placeholder'
The approach makes sure you don't get an IndexError by taking advantage of the fact that empty lists are considered false. It also checks that firstname is in the document so that you don't get a KeyError.

Accessing Nested JSON [AWS Metadata] with Python

I'm using Lambda to run through my AWS account, returning a list of all instances. I need to be able to print out all of the 'VolumeId' values, but I can't work out how to access them as they are nested. I am able to print out the first VolumeId for each instance, however, some of the instances have several volumes, and some only have one. I think I know why I get these results, but I can't work out what to do to get all of them back.
Here's a snippet of what the JSON for one instance looks like:
{
'Groups':[],
'Instances':[
{
'AmiLaunchIndex':0,
'ImageId':'ami-0',
'InstanceId':'i-0123',
'InstanceType':'big',
'KeyName':'nonprod',
'LaunchTime':'date',
'Monitoring':{
'State':'disabled'
},
'Placement':{
'AvailabilityZone':'world',
'GroupName':'',
'Tenancy':'default'
},
'PrivateDnsName':'secret',
'PrivateIpAddress':'1.2.3.4',
'ProductCodes':[
],
'PublicDnsName':'',
'State':{
'Code':80,
'Name':'stopped'
},
'StateTransitionReason':'User initiated',
'SubnetId':'subnet-1',
'VpcId':'vpc-1',
'Architecture':'yes',
'BlockDeviceMappings':[
{
'DeviceName':'/sda',
'Ebs':{
'AttachTime':'date',
'DeleteOnTermination':True,
'Status':'attached',
'VolumeId':'vol-1'
}
},
{
'DeviceName':'/sdb',
'Ebs':{
'AttachTime':'date'),
'DeleteOnTermination':False,
'Status':'attached',
'VolumeId':'vol-2'
}
}
],
This is what I'm doing to get the first VolumeId:
ec2client = boto3.client('ec2')
ec2 = ec2client.describe_instances()
for reservation in ec2["Reservations"]:
for instance in reservation["Instances"]:
instanceid = instance["InstanceId"]
volumes = instance["BlockDeviceMappings"][0]["Ebs"]["VolumeId"]
print("The associated volume IDs for this instance are: ",(volumes))
I think the reason that I'm getting just the first ID is because I'm referencing the first element within "BlockDeviceMappings", but I can't work out how to get the other ones. If I try it without specifying the [0], I get the list indices must be integers or slices, not str error. I tried to use a dictionary instead of a list too, but felt like I was barking up the wrong tree with that one. Any suggestions/help would be appreciated!
One possible answer, not particularly pythonic
...
id_list = []
volumes_data = instance["BlockDeviceMappings"]
for element in volumes_data:
id_list.append(element["Ebs"]["VolumeId"])
Or else use json.loads and then iterate though json using .get syntax like the final answer in this

TypeError : Trouble accessing JSON metadata with Python

So I'm trying to access the following JSON data with python and when i give the statement :
print school['students']
The underlying data gets printed but what I really want to be able to do is print the 'id' value.
{ 'students':[
{
'termone':{
'english':'fifty',
'science':'hundred'
},
'id':'RA1081310005'
}
]
}
So when I do the following I get an error :
print school ['students']['id']
TypeError: list indices must be integers, not str
Can anyone suggest how i can access the ID & where I'm going wrong!
school['students'] is a list. You are trying to access the first element of that list and id key belongs to that element. Instead, try this:
school['students'][0]['id']
Out: 'RA1081310005'
The problem here is that in your list, 'id' is not a part of a dictionary, it is part of a list. To fix this, change your dictionary to the following:
school = {'students':{
'termone': {
"english": "fifty:,
"science": "hundred
},
"id":"RA1081310005"
}
}
Basically, you have a list, and there is no reason to have it, so I removed it.

Python parsing json issue

I'm having troubles parsing a complicated json. That is it:
{
"response": {
"players": [
{
"bar": "76561198034360615",
"foo": "49329432943232423"
}
]
}
}
My code:
url = urllib.urlopen("foobar").read()
js = json.load(url)
data = js['response']
print data['players']
The problem is that this would print the dict. What I want is to reach the key's values, like foo and bar. What I tried so far is doing data['players']['foo'] and it gives me an error that list indices should be integers, I tried of course, it didn't work. So my question is how do I reach these values? Thanks in advance.
data['response']['players'] is an array (as defined by the brackets ([, ]), so you need to access items using a specific index (0 in this case):
data['players'][0]['foo']
Or iterate over all players:
for player in data['response']['players']:
print player['foo']
The problem is that players is a list of items ([ ] in json). So you need to select the first and only item in this case using [0].
print data['players'][0]['foo']
But, keep in mind that you may have more than one player, in which case you either need to specify the player, or loop through the players using a for loop
for player in data['players']:
print player['foo']

Categories