Python JSON parse - Extract attribute - python

Im having more difficulty with this than I should be!
Im trying to extract postalCode from the below bing maps JSON:
{
"authenticationResultCode":"ValidCredentials",
"brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
"copyright":"Copyright © 2014 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
"resourceSets":[
{
"estimatedTotal":1,
"resources":[
{
"__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
"bbox":[
56.216052482429326,
-2.9494141659354827,
56.223777917570679,
-2.9308900340645176
],
"name":"Street, Leven, KY8 5",
"point":{
"type":"Point",
"coordinates":[
56.2199152,
-2.9401521
]
},
"address":{
"addressLine":"Street",
"adminDistrict":"Scotland",
"adminDistrict2":"Fife",
"countryRegion":"United Kingdom",
"formattedAddress":"Street, Leven, KY8 5",
"locality":"Leven",
"postalCode":"KY8 5"
},
"confidence":"Medium",
"entityType":"Address",
"geocodePoints":[
{
"type":"Point",
"coordinates":[
56.2199152,
-2.9401521
],
"calculationMethod":"Interpolation",
"usageTypes":[
"Display",
"Route"
]
}
],
"matchCodes":[
"Good"
]
}
]
}
],
"statusCode":200,
"statusDescription":"OK",
"traceId":"8fdd75362a694e02a45fa17d6e7c0e95|DB40080932|02.00.108.1000|DB4SCH010061257, DB4SCH010061346"
}
My code only returns the field names and not the attribute:
r = requests.get(current_url)
json_data = r.json()
for item in json_data['resourceSets'][0]['resources']:
for field in item['address']:
print field
What am I missing? Sorry for the novice question!

for field in item['address'] by default iterate through the key in item['address'] (a dictionary) only, so you need to:
for item in json_data['resourceSets'][0]['resources']:
for field in item['address']:
print field, item['address'][field]

For loops over dictionaries in Python iterate just over the keys. If you want the values as well, you should use .items():
for field, value in item['address'].items():
print field, value

Related

Get element from python dictionary dump

I am actually working on a rest API functionality and I am able to get the response successfully. I converted the API text response to dictionary. The converted dictionary is actually a bunch of nested dictionaries
The issue I am facing now is I want to access a particular element from the parent dictionary with its name. I can currently fetch it using its index position but going forward the index position might change and hence I need to fetch and process the result using the name.
Sample JSON:
res=response.text
resJSON=json.loads(res)
print(resJSON)
Output:
{
"result": [
{
"serverGroupName": "Ent_Server",
"serverGroupDescription": "Ent Servers",
"serverGroupInstances": []
},
{
"serverGroupName": "db server",
"serverGroupDescription": "Database Servers",
"serverGroupInstances": [
"db1"
]
},
{
"serverGroupName": "default",
"serverGroupDescription": "The default server group.",
"serverGroupInstances": [
"def1"
]
},
{
"serverGroupName": "dvTest",
"serverGroupDescription": "test group",
"serverGroupInstances": [
"a",
"b",
"c",
"d"
]
},
{
"serverGroupName": "wls_Server",
"serverGroupDescription": "weblogic servers",
"serverGroupInstances": [
"wls1"
]
}
]
}
I am interested to retrieve the dictionary item where "serverGroupName": "dvTest" and the list of "serverGroupInstances": ["a","b","c","d"]
I can currently do that using the index position from the JSON dump print(resJSON['result'][3]) but need a more dynamic fetch based on name.
for elem in resJSON["result"]:
if elem["serverGroupName"] == "dvTest":
print(elem)
break
I have put a break statement there assuming you do not need another element with serverGroupName as dvTest. Please comment if you need any more explanation.

Is it possible to retreive object_story_spec for an ad creative which was not created with that object_story_spec? [Python Facebook API]

I have a set of ad creatives that I retreive through the Facebook Business Python SDK. I need these specifically to retreive the outbound URL when someone clicks on the ad: AdCreative['object_story_spec']['video_data']['call_to_action']['value']['link'].
I use the following call:
adcreatives = set.get_ad_creatives(fields=[
AdCreative.Field.id,
AdCreative.Field.name,
AdCreative.Field.object_story_spec,
AdCreative.Field.effective_object_story_id ,
])
Where set is an ad set.
For some cases, the result looks like this (with actual data removed), which is expected:
<AdCreative> {
"body": "[<BODY>]",
"effective_object_story_id": "[<EFFECTIVE_OBJECT_STORY_ID>]",
"id": "[<ID>]",
"name": "[<NAME>]",
"object_story_spec": {
"instagram_actor_id": "[<INSTAGRAM_ACTOR_ID>]",
"page_id": "[<PAGE_ID>]",
"video_data": {
"call_to_action": {
"type": "[<TYPE>]",
"value": {
"link": "[<LINK>]", <== This is what I need
"link_format": "[<LINK_FORMAT>]"
}
},
"image_hash": "[<IMAGE_HASH>]",
"image_url": "[<IMAGE_URL>]",
"message": "[<MESSAGE>]",
"video_id": "[<VIDEO_ID>]"
}
}
}
While sometimes results look like this:
<AdCreative> {
"effective_object_story_id": "[<EFFECTIVE_OBJECT_STORY_ID>]",
"id": "[<ID>]",
"name": "[<NAME>]",
"object_story_spec": {
"instagram_actor_id": "[<INSTAGRAM_ACTOR_ID>]",
"page_id": "[<PAGE_ID>]"
}
}
According to this earlier question: Can't get AdCreative ObjectStorySpec this is due to the fact that the object_story_spec is not populated if it is linked to a creative, instead of created along with the creative.
However, the video_data (and as such, the link), should be saved somewhere. Is there a way to retreive this? Maybe through effective_object_story_id?
The documentation page for object_story_spec (https://developers.facebook.com/docs/marketing-api/reference/ad-creative-object-story-spec/v12.0) does not have the information I am looking for.

Adding OR condition in VersionOne Query API

I am querying the V1 (/query.v1 API) via Python/Dash to get all stories tagged with certain tags.
The Where criteria for API Body is
"where": {
"TaggedWith":"Search-Module" ,
"Team.ID": "Team:009"
},
but I wanted to add OR criteria (something like assets tagged with "Search-Module OR Result-Module")
"where": {
"TaggedWith":"Search-Module;Result-Module" ,
"Team.ID": "Team:009"
},
The documentation in V1 is very basic and I am not able to find the correct way for additional criteria.
https://community.versionone.com/VersionOne_Connect/Developer_Library/Sample_Code/Tour_of_query.v1
Any pointers are appreciated.
You can set alternative values to a variable in the with property and use that variable within the where or filter property values:
{
"from": "Story",
"select": [
"Name"
],
"where": {
"Team.ID": "Team:009",
"TaggedWith": "$tags"
},
"with": {
"$tags": [
"Search-Module",
"Result-Module"
]
}
}
As an option, you can use , (comma) as a separator:
"with": {
"$tags": "Search-Module,Result-Module"
}
The last example of the multi-value variable (but for the rest-1.v1 endpoint) has been found in the VersionOne Grammar project.

Python: Google-Maps-API sends unknown format to parse

I use the Python Client for Google Maps Services to get following data from google-maps:
{
'address_components':[
{
'long_name':'20',
'short_name':'20',
'types':[
'street_number'
]
},
{
'long_name':'Oberböhl',
'short_name':'Oberböhl',
'types':[
'route'
]
},
{
'long_name':'Ingelheim am Rhein',
'short_name':'Ingelheim am Rhein',
'types':[
'locality',
'political'
]
},
{
'long_name':'Mainz-Bingen',
'short_name':'Mainz-Bingen',
'types':[
'administrative_area_level_3',
'political'
]
},
{
'long_name':'Rheinland-Pfalz',
'short_name':'RP',
'types':[
'administrative_area_level_1',
'political'
]
},
{
'long_name':'Germany',
'short_name':'DE',
'types':[
'country',
'political'
]
},
{
'long_name':'55218',
'short_name':'55218',
'types':[
'postal_code'
]
}
],
'adr_address':'<span class="street-address">Oberböhl 20</span>, <span class="postal-code">55218</span> <span class="locality">Ingelheim am Rhein</span>, <span class="country-name">Germany</span>',
'formatted_address':'Oberböhl 20, 55218 Ingelheim am Rhein, Germany',
'formatted_phone_number':'06132 5099968',
'geometry':{
'location':{
'lat':49.9810156,
'lng':8.0739617
},
'viewport':{
'northeast':{
'lat':49.9823942302915,
'lng':8.075293780291501
},
'southwest':{
'lat':49.9796962697085,
'lng':8.072595819708498
}
}
},
'icon':'https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png',
'id':'d2b37ffe23fd5e76648a90df2987558b039fcdf7',
'international_phone_number':'+49 6132 5099968',
'name':'Esch Metalltechnik GmbH',
'place_id':'ChIJHaERGJ_svUcRRfqNoGXq3EU',
'plus_code':{
'compound_code':'X3JF+CH Ingelheim am Rhein, Germany',
'global_code':'8FXCX3JF+CH'
},
'reference':'ChIJHaERGJ_svUcRRfqNoGXq3EU',
'scope':'GOOGLE',
'types':[
'general_contractor',
'point_of_interest',
'establishment'
],
'url':'https://maps.google.com/?cid=5034156205699627589',
'utc_offset':60,
'vicinity':'Oberböhl 20, Ingelheim am Rhein',
'website':'http://www.esch-metalltechnik.de/'
}{
'long_name':'55218',
'short_name':'55218',
'types':[
'postal_code'
]
}
Now I want to extract certain variables, like the "street_number". I don't know which format this data is, so I worked with it like a dictionary:
try:
self.hausnr = place_result_2["address_components"][0]["long_name"]
except:
self.hausnr = "NA"
The problem is, that the index "0" isn't always the same position of the data I want, i varies. Is there a way to extract the data in another way? Perhaps I have to use a JSON-parser or something similar?
Thanks a lot.
The answer is: List comprehensions
try:
# make a list of all address components that have type "street number"
comp = [c for c in place_result_2["address_components"] if "street_number" in c["types"]]
# the first one of them (assuming there will never be more than one) is the desired one
self.hausnr = comp[0]["long_name"]
except:
self.hausnr = "NA"
Since this will probably be a common operation, make a function:
def get_address_component(place_result, comp_type, comp_property="long_name", default=None):
""" returns the first address component of a given type """
try:
comp = [c for c in place_result["address_components"] if comp_type in c["types"]]
return comp[0][comp_property]
except KeyError:
return default
# ...
self.hausnr = get_address_component(place_result_2, "street_number", default="NA")
PS, regarding:
Perhaps I have to use a JSON-parser or something similar?
JSON is a data transfer format - it's plain text. The Google API server used it to get the data across the wire. In your program it has already been parsed - by the Google API client library you are using. What you are looking at is not JSON anymore, it's a Python data structure (nested dicts and lists and values). It just happens to look quite similar to JSON when you print it to the console, because Python uses a similar format to represent data.
On other words, no, you don't need to JSON-parse it again.

Iterating json response from google api using python

I took a nearby location from google api and i get json, when i try to split json i receive error
{
u'status':u'OK',
u'next_page_token':u'CoQC9AAAAHepdLFIvAUuqz6y6WWasKBmq5aAOYr0Bbu97upCMy4EI1Ea5t-6S6iObZdZ5_RIB7ywocdG-lF9ian5JRuTQVGL7MwbBa_uN3EfS7XzjmlVx-IKsauiEiO-Wu3r25zk9SL3yc5d_vDGvN3VQJkA7bBiDWhkloJ4RFngjBsGVWVQOnj5glrbwVVrw9Nu6DNi70C2Wdqqy_65b_jFjJiJYTAwrlfoyl7GGpxk5Gng7QgSFdtTJII9zdfkxcj3osUzklRetjraDtgfaQgxr0KA_H5btbuXz3UT6r-dyqdj2qd1tr_0oAvFkGB9t0qFbUYSe7bDETEAwdDv7MSmmXeYHQUSEMCBruHU5pb8X4EoPbPw9ncaFLgqTTICkQyGYY-boaJ1_3X3SaeT',
u'html_attributions':[
u'Listings by Indiacom Yellow Pages'],
u'results':[
{
u'name':u'Institute for Financial Management and Research',
u'reference':u'CpQBgwAAAL5Gg4T18LzUpNTEzvKWeAH0jLBuTyC_rmxOycL3KndgQ05WVKovVhiIYhnnqeOxcX1tcWesIi0vSVwugaskyy2UnJ_BrTD5ZblXzD7nLxP9L-FOQLetRgbpA6DlNzHM6Nmcu3jtJiBAOyMQJOmgL9cot7c4y18o_3E1cJrzPJfg5hK6trq2u2lvJnD2ZxJ6IxIQC2IuHwQILkrbtUd3ke5GDBoU1sZLoPY-_kARc7lEoq2naKHtwSk',
u'geometry':{
u'location':{
u'lat':13.062882,
u'lng':80.238669
}
},
u'place_id':u'ChIJKzE7o2ZmUjoRLaCtNPjba3U',
u'vicinity':u'24,
Kothari Road,
Nungambakkam,
Chennai',
u'photos':[
{
u'photo_reference':u'CnRoAAAApH-YJpJFjPYltZYhYTs_tIVFA7vve-LMii8XbUydZJLMXbzDNkxuCuGCk9W-nFjgUrj-JoRqJLRuurGvt1oz94osENNc8bZGLBI4Joj1w-dQSyiwqqzqDdna-u0TRkJ_8S91fF3uerww341951YB2hIQX7gFjIn5tWkkEcGwErJ9oBoU0CdKRd6b2pL3Bcp09hCYvleEfaQ',
u'width':816,
u'html_attributions':[
u'From a Google User'
],
u'height':459
}
],
u'scope':u'GOOGLE',
u'id':u'2e9a63cf7368e0f90e2a20711ac56853b7c34462',
u'types':[
u'school',
u'establishment'
],
u'icon': u'http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png'
},
{
u'rating':4.2,
u'name':u'Sri Sankara Senior Secondary School',
u'reference':u'CoQBdgAAAJ-Uc78EbPnLX6adzheZMWrS9sOJ9vWTQsqZOlQza-r3qozDUrl4XxWPRdHD9K_BVP0t_FhEwQt4w42X0z01uQr7dtq5cZ7ioa9zBVIQpwOkSQhxjbjQjX05YxVqGPB9MCfEikHpFKSKIaz5mPrLDgklbhQ8clD4fm9BiWNmE_mJEhD35R4GgbVNu4J-x0Lfaw3BGhRPQEXErZf3jJJkLbHs2HWVRvP2Xg',
u'geometry':{
u'location':{
u'lat':13.009931,
u'lng':80.260746
}
},
u'place_id':u'ChIJh_fXcelnUjoRd4vKDQfY_DM',
u'vicinity':u'9/21 Vasantha Press Road,
Vasanta Press Road,
Adyar,
Landmarks are Malar Hospital/Theosophical society,
Chennai',
u'photos':[
{
u'photo_reference':u'CnRwAAAAIrFQSUJn7JB5_GgDfEPBldHptKmARqhV-6HR5fUT-MjB6ScO7ZYz1jamqoGvTqXlbEZZjxC67BvOllBHTiRIQwKyBXoI9DhleBmrCgMTrorjeDkvIDY_8ZC0pOFZOZGGH2XdfLrH1irsWZUEa0IjFRIQaATxA2BymP1KED4vxNZfnxoUTwD5Y-4-8ZPnPrhuKofUVSztcoQ',
u'width':297,
u'html_attributions':[
],
u'height':297
}
],
u'scope':u'GOOGLE',
u'id':u'a8dc412bac3ea790260d2c7d6fe08271ae883a4e',
u'types':[
u'school',
u'establishment'
],
u'icon': u'http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png'
},
{
u'rating':4.5,
u'name':u'Chettinad Vidyashram',
u'reference':u'CnRoAAAAbIG1-6ecTuOcqw5hCenhtbHlAmP-nfdw_W1vEv94fXvIyCzhHSQMn95VEtKCgbLeME6qd30uGhxmLxFwXItcls-SlC7fgXwGl2JINCLTjB1RYpYC--Gr6hS-9cT7Xq2f46-dAqnpF5n2sRa1cNJJSBIQvVLDztqmh2BmqkJER9MLZxoU3gbS1TpgVj8h5Uo71QKTTyj1CdQ',
u'geometry':{
u'location':{
u'lat':13.017083,
u'lng':80.269503
}
},
u'place_id':u'ChIJR3w9SdxnUjoRs2vfnH-ERNA',
u'vicinity':u'Rajah Annamalaipuram,
Chennai',
u'photos':[
{
u'photo_reference':u'CnRoAAAAeUHwPDKO87eeGP7Fzm7aKE3VcQd6gFebbjo2FhYRHdulLZW-XdepstzETly74Id6NMOF5lqm4BHZ56C1CRnsxmdqaxJ-rcJR2Cpq2VfJaixZmBG3C-0TTNmMuPuGsjKAldr6rWCWdDVMg8FAnWhgyRIQXYPX89XdA5fl7e5RUecRWhoU-SExDqUr-GRaYVLkb8Iq_1mf-R8',
u'width':968,
u'html_attributions':[
u'From a Google User'
],
u'height':968
}
],
u'scope':u'GOOGLE',
u'id':u'f3b774d4c11a4bd20585669d9c4ae57fc12e5652',
u'types':[
u'school',
u'establishment'
],
u'icon': u'http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png'
},
Here is my python code
res = json.dumps(response)
for result in response[status][results]:
print result['status']
as json was big i put half json data only.
Error i get is ror at 1431:global name 'status' is not defined
How to split this json
When i print type(response)
type tuple
If you get a json string you want to load it into the native structure with loads and then iterate over that native structure. Looking at the json string you have it also seems as if the individual results don't have a status field. You could do something like this:
res = json.loads(response)
print res['status']
for result in res['results']:
print result

Categories

Resources