Home > Enterprise >  python how to get dictionally object in json?
python how to get dictionally object in json?

Time:01-29

I start learning json. I can access title object like this

json_data = results['local_results']
for i in json_data:
    if "title" in i:
        title = i["title"]

How to accesss latitude object of gps_coordinates, website object of links.

here is my json data.

"local_results": [
    {
      "position": 1,
      "title": "McDonald's",
      "place_id": "2142927458143177356",
      "lsig": "AB86z5W5r155sIcs3jqfYkm9Y8Fp",
      "place_id_search": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=austin, texas, united states&lsig=AB86z5W5r155sIcs3jqfYkm9Y8Fp&ludocid=2142927458143177356&q=McDonald's&tbm=lcl&token=16c37bb72affc4f2",
      "address": "Austin, TX",
      "phone": "(512) 442-0412",
      "hours": "Open ⋅ Closes 12AM",
      "gps_coordinates": {
        "latitude": 30.260337999999994,
        "longitude": -97.7581347
      },
      "links": {
        "website": "https://www.mcdonalds.com/us/en-us/location/TX/AUSTIN/1209-BARTON-SPRINGS/4941.html?cid=RF:YXT:GMB::Clicks",
        "directions": "https://www.google.com/maps/dir//McDonald's, 1209 Barton Springs Rd, Austin, TX 78704/data=!4m6!4m5!1m1!4e2!1m2!1m1!1s0x8644b51bd54fc423:0x1dbd352b620e0a8c?sa=X&hl=en"
      }
    },

CodePudding user response:

Json data:

    x = {"local_results": [
    {
      "position": 1,
      "title": "McDonald's",
      "place_id": "2142927458143177356",
      "lsig": "AB86z5W5r155sIcs3jqfYkm9Y8Fp",
      "place_id_search": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=austin, texas, united states&lsig=AB86z5W5r155sIcs3jqfYkm9Y8Fp&ludocid=2142927458143177356&q=McDonald's&tbm=lcl&token=16c37bb72affc4f2",
      "address": "Austin, TX",
      "phone": "(512) 442-0412",
      "hours": "Open ⋅ Closes 12AM",
      "gps_coordinates": {
        "latitude": 30.260337999999994,
        "longitude": -97.7581347
      },
      "links": {
        "website": "https://www.mcdonalds.com/us/en-us/location/TX/AUSTIN/1209-BARTON-SPRINGS/4941.html?cid=RF:YXT:GMB::Clicks",
        "directions": "https://www.google.com/maps/dir//McDonald's, 1209 Barton Springs Rd, Austin, TX 78704/data=!4m6!4m5!1m1!4e2!1m2!1m1!1s0x8644b51bd54fc423:0x1dbd352b620e0a8c?sa=X&hl=en"
      }
    }]}

Coordinates extraction:

x['local_results'][0]['gps_coordinates']

Ouput:

{'latitude': 30.260337999999994, 'longitude': -97.7581347}

Website extraction:

print(x['local_results'][0]['links']['website'])

output:

https://www.mcdonalds.com/us/en-us/location/TX/AUSTIN/1209-BARTON-SPRINGS/4941.html?cid=RF:YXT:GMB::Clicks

CodePudding user response:

Here is your JSON data:

   x = {"local_results": [
        {
          "position": 1,
          "title": "McDonald's",
          "place_id": "2142927458143177356",
          "lsig": "AB86z5W5r155sIcs3jqfYkm9Y8Fp",
          "place_id_search": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=austin, texas, united states&lsig=AB86z5W5r155sIcs3jqfYkm9Y8Fp&ludocid=2142927458143177356&q=McDonald's&tbm=lcl&token=16c37bb72affc4f2",
          "address": "Austin, TX",
          "phone": "(512) 442-0412",
          "hours": "Open ⋅ Closes 12AM",
          "gps_coordinates": {
            "latitude": 30.260337999999994,
            "longitude": -97.7581347
          },
          "links": {
            "website": "https://www.mcdonalds.com/us/en-us/location/TX/AUSTIN/1209-BARTON-SPRINGS/4941.html?cid=RF:YXT:GMB::Clicks",
            "directions": "https://www.google.com/maps/dir//McDonald's, 1209 Barton Springs Rd, Austin, TX 78704/data=!4m6!4m5!1m1!4e2!1m2!1m1!1s0x8644b51bd54fc423:0x1dbd352b620e0a8c?sa=X&hl=en"
          }
        }]}

You enter JSON information into this website (http://jsonviewer.stack.hu/) and you see your data regularly and you can easily access it.

so, here you can access the link and gps_coordinates this way:

print(x['local_results'][0]['gps_coordinates'])
print(x['local_results'][0]['links']) 
  •  Tags:  
  • Related