<jupyter_start><jupyter_text>Loading the Data FileThe code below creates a function that parses a data file (provided as a paramater to the function) and returns the contents of the file back as a Python object (a dictionary, specifically). To do this, we need to use the Python `json` library, so we neeed to import that first, then we can create our function to use it.<jupyter_code>import json           # use the json library

def loadData(file):   # define a function (called loadData) that takes a parameter named file
  f = open(file, 'r') # open the file (specified in the parameter) in read-only mode
  return json.load(f) # use the json library to load the file and return the data<jupyter_output><empty_output><jupyter_text>Calling the functionNow that the function has been created, let's call it using the code below. We set a variable named `dataFile` to the location of the data file on the server (which is `/data/urls.json`).Then we call the `loadData` function, using the variable name we created as a parameter.After the function runs, we just print out the data that it returned (which is stored in the variable named `urls`).<jupyter_code>dataFile = "/data/urls.json" # create a variable that points to the data file on the server
urls = loadData(dataFile)    # call the loadData function, passing the dataFile as a parameter
print (urls)                 # print out the results from the loadData function<jupyter_output>{'utils': {'ip2country': 'https://api.ip2country.info/ip', 'randomuser': 'https://randomuser.me/api/', 'uspopulation': 'https://datausa.io/api/data?drilldowns=Nation&measures=Population'}, 'nasa': {'issloc': 'http://api.open-notify.org/iss-now.json', 'peopleinspace': 'http://api.open-notify.org/astros.json'}, 'fun': {'randomcat': 'https://cataas.com/cat?json=true', 'randomjoke': 'https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,racist,sexist,explicit'}}<jupyter_text>Accessing specific dataNow that we have the data from the file (stored in `urls`), we can access specific data we want by using that. For example, if we want to get the URL for the NASA API that lets us get the current location of the International Space Station (ISS), we see that is stored in the data file in the `nasa` => `iss_loc` field. We can use that information to access the data (and store it as a new `apiUrl` variable), as shown below:<jupyter_code>apiUrl = urls['nasa']['iss_loc'] # create a variable that refers to the specific data we want
print (apiUrl)                   # print the value of the data (the url for the API)<jupyter_output>http://api.open-notify.org/iss-now.json<jupyter_text>Using the APILet's create a function that takes a URL as a parameter, and makes an HTTP request to that URL and returns the data from it as a Python object. We use the `requests` library in this example.<jupyter_code>import requests           # use the requests library

def getApiData(url):      # create a function called getApiData that takes a url as a parameter
  res = requests.get(url) # use the requests get() function to access the API url
  return res.json()       # return the data from the web request<jupyter_output><empty_output><jupyter_text>Getting the API dataNow that we have our function created, let's call it using the `apiUrl` variable we set earlier:<jupyter_code>apiData = getApiData(apiUrl) # set a variable that contains the data from the API request
print (apiData)              # print the value of the variable (the API data)<jupyter_output>{'timestamp': 1636568881, 'message': 'success', 'iss_position': {'latitude': '48.7612', 'longitude': '126.1685'}}<jupyter_text>Putting it all togetherClearing out the documentation and creating a single, concise script gives us this:<jupyter_code>import json
import requests

dataFile = "/data/urls.json"

def loadData(file):
    f = open(file, 'r')
    return json.load(f)

def getApiData(url):
    res = requests.get(url)
    return res.json()

urls = loadData(dataFile)
apiUrl = urls['nasa']['issloc']
apiData = getApiData(apiUrl)

print (apiData)<jupyter_output>{'timestamp': 1636568895, 'message': 'success', 'iss_position': {'latitude': '48.4717', 'longitude': '127.3102'}}
{'fraction_non_alphanumeric': 0.1003833253473886, 'fraction_numerical': 0.011739338763775755, 'mean_word_length': 4.703551912568306, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '565291', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1223, 'n_words': 512, 'path': 'examples/apiCalls.ipynb', 'repo_name': 'rossja/badcode.world', 'size': '4174'}