Home > Back-end >  Cannot parse JSON data returned between [{ and }] Python
Cannot parse JSON data returned between [{ and }] Python

Time:10-14

Here's where I'm starting from:

url = 'https://api.example.com/'
bearer_token = 'my_token'

result = requests.get(url,headers={'Content-Type':'application/json','Authorization': 'Bearer {}'.format(bearer_token)}).json()
print(result[3])

When I print the type of result my JSON results produce, I get a 'list', but I can't parse through it the way one normally would a list. For example, I can't access keys behind [ and {. These areas seem to be treated differently when I try to parse data from them. Some values are accessible by [index]['key'], others are not.

Here's a snippet of my JSON output when I print(result[3]):

{'created_at': 'Wed Oct 1 10:20:16  0000 2021', 'id': 1448292507, 'id_str': '1448817927', 'text': 'Lorem ipsum data data data', 'truncated': True, 'entities': {'hashtags': [{'text': 'RedLife', 'indices': [90, 101]}, {'text': 'NS18', 'indices': [102, 107]}], 'symbols': [], 'user_ment': [{'n_name': 'name_1', 'name': 'name_2', 'id': 5232, 'id_str': '32', 'indices': [12, 10]}, {'n_name': 'Lorem', 'name': 'Lorem', 'id': 1372, 'id_str': '72', 'indices': [10, 17]}, {'n_name': 'TV', 'name': 'TV', 'id': 3576, 'id_str': '76', 'indices': [55, 60]}], 'urls': [{'url': 'https://website.com', 'expanded_url': 'https://website'...}

When I print(result[3]['created_at']), I get Wed Oct 1 10:20:16 0000 2021. Great!

When I try other keys in the list I get KeyErrors.

What do I need to do to my JSON data in order to pull all the data I need? And in the case where there exists multiple instances of the same 'key', how do I retrieve the 'value' I'm looking for? Do I just append the index I want like this: print(result[3]['n_name'][0])?

CodePudding user response:

To access n_name, you're missing 'entities' and 'user_ment' levels in the dict, then you have a list, then again a list

result = [
    {},
    {},
    {},
    {'created_at': 'Wed Oct 1 10:20:16  0000 2021', 'id': 1448292507,
     'id_str': '1448817927',
     'text': 'Lorem ipsum data data data', 'truncated': True,
     'entities': {
         'hashtags': [{'text': 'RedLife', 'indices': [90, 101]}, {'text': 'NS18', 'indices': [102, 107]}],
         'symbols': [],
         'user_ment': [
             {'n_name': 'name_1', 'name': 'name_2', 'id': 5232, 'id_str': '32', 'indices': [12, 10]},
             {'n_name': 'Lorem', 'name': 'Lorem', 'id': 1372, 'id_str': '72', 'indices': [10, 17]},
             {'n_name': 'TV', 'name': 'TV', 'id': 3576, 'id_str': '76', 'indices': [55, 60]}
         ],
         'urls': [{'url': 'https://website.com', 'expanded_url': 'https://website'}]
     }}
]

x = result[3]['entities']['user_ment'][0]['n_name']
print(x)  # name_1

x = result[3]['entities']['user_ment'][1]['n_name']
print(x)  # Lorem

CodePudding user response:

You can parse through the data as long as you start with an integer index first.

For starters, make sure you can see how the lists are structured in an easily readable, organized way. I pasted my JSON results into https://jsonformatter.curiousconcept.com/#

Locate where your keys are in the list or nested lists. Mine happened to be in the first list, so I'm taking result from:

result = requests.get(url,headers={'Content-Type':'application/json','Authorization': 'Bearer {}'.format(bearer_token)}).json()

And printing:

print(result[0]['entities']['user_ment'][0]['n_name'])
  • Related