We can view the contents of the data object by typing data as in the code block above. Then, we will store the resulting JSON data in a dictionary object named data. We can use the get() method to submit the URL for our API call. Let’s begin by importing the requests package. Now, we are ready to submit our API call to the openFDA data server. > +AND+occurcountry:"US"+AND+_name:"Fentanyl" > URL = API + date + "+AND+" + country + "+AND+" + drug + "&" + data Our code block is still easy to read even though the URL for our API call has become quite complex. URL = API + date + "+AND+" + country + "+AND+" + drug + "&" + data Note that the string data must be preceded by & rather than +AND+. The string data in the code block below contains the specific syntax and must be added to the end of the URL for the API call. URL = API + date + "+AND+" + country + "+AND+" + drugįinally, let’s specify that our results contain data for the number of adverse events that occurred on each day. The string drug in the code block below includes the syntax that specifies that the adverse event involved Fentanyl. We can use a similar strategy to further restrict our query to adverse events that involved the drug Fentanyl. Our code remains easy to read even as the URL for our API call becomes more complex. Note that we must include “+AND+” between date and country when we define URL. Then, we can combine the strings API, date, and country to specify the complete API call stored in the string URL. In the code block below, the string country contains syntax that restricts our query to the United States. Let’s further restrict our query to adverse events that occurred in the United States from January 1, 2018, through January 5, 2018. Our Python code block is easier to read, and the URL for the API call is still the same. Then, we can combine the strings by typing URL = API + date. The string API contains the URL for the basic API call, and the string date narrows our search from January 1, 2018, through January 5, 2018. Let’s split the URL into two strings: API and date. The syntax works, but we have added only one search field, and the URL for the API call is already difficult to read. Let’s restrict our search to adverse events reported from January 1, 2018, through January 5, 2018, by adding the ?search option to our API call in the code block below. You can see the syntax, a list of searchable fields, and examples on the openFDA website. We can customize our data request by adding search fields and values to our API call. The statement URL in the third line of the code block above displays the contents of the string URL. The string URL contains the URL to request adverse event data using the openFDA API. Let’s begin by defining a string named URL within a Python code block. We will be using the requests and pandas packages, so you should check that they are installed before we begin. Our goal is to use the openFDA API to request data about adverse drug events and convert the nested JSON data to a Stata dataset. There are two keys at the top of the JSON nesting structure: meta: and results. In the image above, the time: key is nested within the 4: key, and the 4: key is nested within the results: key. For example, time: “20180105” is nested within the 4: key. The key is time and the value is “20180105”. The image above includes the key:value pair time: “20180105” near the bottom. A “key” is similar to a variable in a Stata dataset, and a “value” is the data. JSON is a popular data file format that consists of a collection of key:value pairs. We can type the URL for this API call in the address bar of a web browser, and the browser will display the resulting data as a JSON file. For example, the URL below requests the number of adverse events in the United States that involved Fentanyl from January 1, 2018, through January 5, 2018. We can add options to our API call to narrow our data request. For example, the URL below uses the openFDA API to request data about adverse drug events from the United States Food and Drug Administration. But a typical API consists of a URL followed by query options. There are many different kinds of APIs, and the syntax is often unique to each API. If you are not familiar with Python, it may be helpful to read the first four posts in my Stata/Python Integration series before you read further.Īn API is a software application that can be used to request data from another computing system. In this post, I will show you how to use Python to request data with API calls and how to work with the resulting JSON data. APIs often return the requested data in a JavaScript Object Notation (JSON) file. Many government agencies, financial institutions, universities, and social media platforms provide access to their data through an application programming interface (API).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |