Home > Software design >  Navigate in JSON with nultiple keys
Navigate in JSON with nultiple keys

Time:12-05

I'm trying to get a key from a JSON from a website using the following code:

import json
import requests
from bs4 import BeautifulSoup

url = input('Enter url:')
html = requests.get(url)
soup = BeautifulSoup(html.text,'html.parser')

data = json.loads(soup.find('script', type='application/json').text)
print(data)
print("####################################")

And here is the JSON:

{"props": {
    "XYZ": {
        "ABC": [
            {
                "current": "sold",
                 "location": "FD",
                 "type": "d",
                 "uid": "01020633"
            }
        ],
        "searchTerm": "asd"
    }
}}

I'm able to load the page, find the JSON, and print all data. The question is, how can I print only the information from the current key? Will something like the following work?

print(data['props']['XYZ']['ABC']['current']

CodePudding user response:

You can access it like this:

data["props"]["XYZ"]["ABC"][0]["current"]

Why? Key current is inside a list of dictionaries. ABC is of type list, and we access the elements using their location in the list (0 in your example).

CodePudding user response:

As the other answers have already explained, you need to add [0] between ['ABC'] and ['current'] because the value that corresponds to the "ABC" key is a list containing a dictionary with the "current" key, so you can access it with

data["props"]["XYZ"]["ABC"][0]["current"]

Also, if you have a very complex and nested data structure and you want to quickly you can use this set of functions

getNestedVal(data, 'current')

prints sold, and

getNestedVal(data, 'current', 'just_expr', 'data')

prints data["props"]["XYZ"]["ABC"][0]["current"] so that you can copy it from the terminal and use in your code. (It's not the best idea to use it other than for just figuring out data structure since it can use up quite a bit of time and memory.)

CodePudding user response:

You can access current like below:

data["props"]["XYZ"]["ABC"][0]["current"]

you can get a list of dictionaries by accessing "ABC" key's value. So whenever you get curly {} bracket it shows that it is a dictionary and you can access value of particular keys using dict[key] or dict.get(key,default_value) and whenever you get square [] bracket it shows that it is list and you can access its element by index list[0],list[1].

So, accessing list element and dictionary's key's value is two different things. I hope you understood my point.

  • Related