Home > Enterprise >  Iterating through a JSON object using a FOR loop
Iterating through a JSON object using a FOR loop

Time:11-02

I'm currently in a data analytics boot camp, and have had a TON of information thrown at me in the span of 6 weeks. Working in python, and juypter notebooks. I'm having some issues running a FOR loop on a JSON object. Specifically looking to pull the 6 'full_name' out of each different 'padid' in the json, and print them in order. I just can't seem to get my poor tired brain to wrap around what syntax i need to get it to run. Any advice or insight would be hugely helpful!

import requests
import json

url = "https://api.spacexdata.com/v2/launchpads"

space = requests.get(url)

spacex= space.json()
spacex

[{'padid': 5,
  'id': 'vafb_slc_3w',
  'name': 'VAFB SLC 3W',
  'full_name': 'Vandenberg Air Force Base Space Launch Complex 3W',
  'status': 'retired',
  'location': {'name': 'Vandenberg Air Force Base',
   'region': 'California',
   'latitude': 34.6440904,
   'longitude': -120.5931438},
  'vehicles_launched': ['Falcon 1'],
  'attempted_launches': 0,
  'successful_launches': 0,
  'wikipedia': 'https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_3',
  'details': 'SpaceX original west coast launch pad for Falcon 1. Performed a static fire but was never used for a launch and abandoned due to scheduling conflicts.'},
 {'padid': 2,
  'id': 'ccafs_slc_40',
  'name': 'CCAFS SLC 40',
  'full_name': 'Cape Canaveral Air Force Station Space Launch Complex 40',
  'status': 'active',
  'location': {'name': 'Cape Canaveral',
   'region': 'Florida',
   'latitude': 28.5618571,
   'longitude': -80.577366},
  'vehicles_launched': ['Falcon 9'],
  'attempted_launches': 61,
  'successful_launches': 59,
  'wikipedia': 'https://en.wikipedia.org/wiki/Cape_Canaveral_Air_Force_Station_Space_Launch_Complex_40',
  'details': 'SpaceX primary Falcon 9 launch pad, where all east coast Falcon 9s launched prior to the AMOS-6 anomaly. Initially used to launch Titan rockets for Lockheed Martin. Back online since CRS-13 on 2017-12-15.'},
 {'padid': 8,
  'id': 'stls',
  'name': 'STLS',
  'full_name': 'SpaceX South Texas Launch Site',
  'status': 'under construction',
  'location': {'name': 'Boca Chica Village',
   'region': 'Texas',
   'latitude': 25.9972641,
   'longitude': -97.1560845},
  'vehicles_launched': ['Falcon 9'],
  'attempted_launches': 0,
  'successful_launches': 0,
  'wikipedia': 'https://en.wikipedia.org/wiki/SpaceX_South_Texas_Launch_Site',
  'details': 'SpaceX new launch site currently under construction to help keep up with the Falcon 9 and Heavy manifests. Expected to be completed in late 2018. Initially will be limited to 12 flights per year, and only GTO launches.'},
 {'padid': 1,
  'id': 'kwajalein_atoll',
  'name': 'Kwajalein Atoll',
  'full_name': 'Kwajalein Atoll Omelek Island',
  'status': 'retired',
  'location': {'name': 'Omelek Island',
   'region': 'Marshall Islands',
   'latitude': 9.0477206,
   'longitude': 167.7431292},
  'vehicles_launched': ['Falcon 1'],
  'attempted_launches': 5,
  'successful_launches': 2,
  'wikipedia': 'https://en.wikipedia.org/wiki/Omelek_Island',
  'details': 'SpaceX original launch site, where all of the Falcon 1 launches occured. Abandoned as SpaceX decided against upgrading the pad to support Falcon 9.'},
 {'padid': 6,
  'id': 'vafb_slc_4e',
  'name': 'VAFB SLC 4E',
  'full_name': 'Vandenberg Air Force Base Space Launch Complex 4E',
  'status': 'active',
  'location': {'name': 'Vandenberg Air Force Base',
   'region': 'California',
   'latitude': 34.632093,
   'longitude': -120.610829},
  'vehicles_launched': ['Falcon 9'],
  'attempted_launches': 16,
  'successful_launches': 16,
  'wikipedia': 'https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_4',
  'details': 'SpaceX primary west coast launch pad for polar orbits and sun synchronous orbits, primarily used for Iridium. Also intended to be capable of launching Falcon Heavy.'},
 {'padid': 4,
  'id': 'ksc_lc_39a',
  'name': 'KSC LC 39A',
  'full_name': 'Kennedy Space Center Historic Launch Complex 39A',
  'status': 'active',
  'location': {'name': 'Cape Canaveral',
   'region': 'Florida',
   'latitude': 28.6080585,
   'longitude': -80.6039558},
  'vehicles_launched': ['Falcon 9', 'Falcon Heavy'],
  'attempted_launches': 27,
  'successful_launches': 26,
  'wikipedia': 'https://en.wikipedia.org/wiki/Kennedy_Space_Center_Launch_Complex_39#Launch_Complex_39A',
  'details': 'NASA historic launch pad that launched most of the Saturn V and Space Shuttle missions. Initially for Falcon Heavy launches, it is now launching all of SpaceX east coast missions due to the damage from the AMOS-6 anomaly. After SLC-40 repairs are complete, it will be upgraded to support Falcon Heavy, a process which will take about two months. In the future it will launch commercial crew missions and the Interplanetary Transport System.'}]

CodePudding user response:

spacex is simply a list of dictionaries. You iterate through it like any other list.

for site in spacex:
    print(site['full_name'])
  • Related