skip to Main Content

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']

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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')
    

    returns 'sold', and

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

    returns '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.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search