简体   繁体   中英

How to select a specific field in json data in python

I am new to handling data in json file and i am trying to extract specific information ( "expanded_url" in my case). when i am loading the json file as string using json.loads() i cannot select expanded_url data from the list of stings. i am proving my json data drive link for your reference.

My query: i want to extract only "expanded_url" field mentioned in entity data (it may or may not be empty) from the json file.

json data:

https://drive.google.com/file/d/1Hgnwi74JTECY4cQRp3rG3zX4DX1dGa9A/view?usp=sharing

After you converted your json data via json.loads you able to manipulate with data via dictionary data type.

And a little example how you can to extract expanded_url values from your json data.

def get_by_key(obj):
    return obj["quoted_status"]["extended_tweet"]["extended_entities"]

# `data` means your dictionary object.
media_objects = [get_key(media) for media in data]


for media_obj in media_objects:
    for url in media_obj['media']:
        print(url['expanded_url'])

Useful link , how to work with json in Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM