Skip to content Skip to sidebar Skip to footer

Python Extract Elements From Json String

I have a Json string from which I'm able to extract few components like formatted_address,lat,lng, but I'm unable to extract feature(values) of other components like intersection,

Solution 1:

I would go for json_normalize, thought of one line answer but I dont think its possible i.e (Here I did only for px_val and py_val you can do similar things for other columns)

from pandas.io.json import json_normalize

import pandas as pd
import json

withopen('dat.json') as f:
    data = json.load(f)

result = json_normalize(data,'results')

result['px_val'] = result['geometry'].apply(json_normalize).apply(lambda x : x['location.lat'])
result['py_val'] = result['geometry'].apply(json_normalize).apply(lambda x : x['location.lng'])

print(result[['formatted_address','px_val','py_val']])
                              formatted_address        px_val      py_val
0  Satya Niwas, Kanti Nagar, J B Nagar, Andheri E...  19.109659  72.867471
1  82, Bamanpuri, Ajit Nagar, J B Nagar, Andheri ...  19.109749  72.867249
2  Todi Building, Sheth Bhavanidas Benani Marg, K...  19.109827  72.867789
3  KASI APARTMENTS, Shriniwas Bagarka Rd, Bamanpu...  19.109334  72.867052
4  Silver Line Apts., Bamanpuri, J.B. Nagar, J B ...  19.109108  72.867078
5  Gokul panch chs, 81-B, 81-B, Sheth Bhavanidas ...  19.109871  72.867057
6  Ajit Nagar, J B Nagar, Andheri East, Mumbai, M...  19.110316  72.868073
7  Bamanpuri, J B Nagar, Andheri East, Mumbai, Ma...  19.108435  72.865749
8  J B Nagar, Andheri East, Mumbai, Maharashtra 4...  19.111062  72.865592
9           Andheri East, Mumbai, Maharashtra, India  19.115491  72.872695

I try to parse political certainly not proud of this solution i.e

pol = []
for i inresult['address_components'].apply(json_normalize):
     pol.append(','.join(i.apply(lambda x : x['long_name'] if 'political'in x['types'] else np.nan,1).dropna()))

result['political'] = pol

Output result['political']

0    Kanti Nagar,J B Nagar,Andheri East,Mumbai,Mumb...
1    Bamanpuri,Ajit Nagar,J B Nagar,Andheri East,Mu...
2    Kanti Nagar,J B Nagar,Andheri East,Mumbai,Mumb...
3    Bamanpuri,Kanti Nagar,J B Nagar,Andheri East,M...
4    Bamanpuri,J.B. Nagar,J B Nagar,Andheri East,Mu...
5    Bamanpuri,Ajit Nagar,J B Nagar,Andheri East,Mu...
6    Ajit Nagar,J B Nagar,Andheri East,Mumbai,Mumba...
7    Bamanpuri,J B Nagar,Andheri East,Mumbai,Mumbai...
8    J B Nagar,Andheri East,Mumbai,Mumbai Suburban,...
9    Andheri East,Mumbai,Mumbai Suburban,Maharashtr...
Name: political, dtype: object

To convert it to a method we can do

defget_cols(st):
    pol = []
    for i in result['address_components'].apply(json_normalize):
         pol.append(','.join(i.apply(lambda x : x['long_name'] if st in x['types'] else np.nan,1).dropna()))

   return  pol

result['political'] = get_cols('political') 
# This will assign the new column political with data. 

Solution 2:

This is rather a pretty broad question...

To help you to start:

record_path = ['address_components']

meta= [
  'formatted_address',
  ['geometry','location','lat'],
  ['geometry','location','lng'],  
]

x = pd.io.json.json_normalize(d['results'], record_path, meta)

Result:

In [20]: pd.options.display.max_rows =15In [21]: x
Out[21]:
          long_name       short_name                                          types  \
0       Satya Niwas      Satya Niwas    [establishment, point_of_interest, premise]
1       Kanti Nagar      Kanti Nagar  [political, sublocality, sublocality_level_3]
2         J B Nagar        J B Nagar  [political, sublocality, sublocality_level_2]
3      Andheri East     Andheri East  [political, sublocality, sublocality_level_1]
4            Mumbai           Mumbai                          [locality, political]
5   Mumbai Suburban  Mumbai Suburban       [administrative_area_level_2, political]
6       Maharashtra               MH       [administrative_area_level_1, political]
..              ...              ...                                            ...
83            India               IN                           [country, political]
84400047400047                                  [postal_code]
85     Andheri East     Andheri East  [political, sublocality, sublocality_level_1]
86           Mumbai           Mumbai                          [locality, political]
87  Mumbai Suburban  Mumbai Suburban       [administrative_area_level_2, political]
88      Maharashtra               MH       [administrative_area_level_1, political]
89            India               IN                           [country, political]

                                    formatted_address  geometry.location.lat  geometry.location.lng
0   Satya Niwas, Kanti Nagar, J B Nagar, Andheri E...              19.10965972.8674711   Satya Niwas, Kanti Nagar, J B Nagar, Andheri E...              19.10965972.8674712   Satya Niwas, Kanti Nagar, J B Nagar, Andheri E...              19.10965972.8674713   Satya Niwas, Kanti Nagar, J B Nagar, Andheri E...              19.10965972.8674714   Satya Niwas, Kanti Nagar, J B Nagar, Andheri E...              19.10965972.8674715   Satya Niwas, Kanti Nagar, J B Nagar, Andheri E...              19.10965972.8674716   Satya Niwas, Kanti Nagar, J B Nagar, Andheri E...              19.10965972.867471
..                                                ...                    ...                    ...
83  J B Nagar, Andheri East, Mumbai, Maharashtra 4...              19.11106272.86559284  J B Nagar, Andheri East, Mumbai, Maharashtra 4...              19.11106272.86559285           Andheri East, Mumbai, Maharashtra, India              19.11549172.87269586           Andheri East, Mumbai, Maharashtra, India              19.11549172.87269587           Andheri East, Mumbai, Maharashtra, India              19.11549172.87269588           Andheri East, Mumbai, Maharashtra, India              19.11549172.87269589           Andheri East, Mumbai, Maharashtra, India              19.11549172.872695

[90rows x 6 columns]

Solution 3:

You need to understand schema of your data.

Error in json_st['results']['address_components']

because json_st['results'] is an array

Check it here http://jsoneditoronline.org

Here is some sample

for result in data['results']:
    printtype(result)
    for address_component in result['address_components']:
        printtype(address_component)
        print address_component['long_name']
        print address_component['short_name']
        for _typein address_component['types']:
            print _type

Post a Comment for "Python Extract Elements From Json String"