Python script to extract data from API and write into json file

An API is a set of routines, protocols and tools used to create software applications. Basically, an API specifies the interaction of software components.An application programming interface describes the interactions between multiple software intermediaries. It specifies the types of calls or requests to be made, how to do them, the data formats to be used, the conventions to be followed etc.

Clean, structured data with little human intervention or site-specific training will be collected by data extraction using the API.You compile a profile or data request using the Data Extraction API in accordance with REST standards. This framework is a combination of URL parameters.


Upon requesting a URL, the receiving website would most likely send a reply, which could be the content requested or an error describing the failed request. Data-based responses are in HTML or XML or even JSON scripts that need readability cleaning.

Below is the example where we are extracting data related to Apple stock price using REST API in Python and the writing the output in json file format. Below code will generate the output file at your server location is json format.

import requests
import pandas as pd
import json 


# Python program to write JSON 
# to a file 
# Writing to stock.json 
with open("stock.json", "w") as outfile: 
	 json.dump( r.json(), outfile)

Here in this example i am using API to extract the date related to stock market and in API url i have passed filter symbols=aapl which will give the data of APPLE company.

Please note: I have changed the access key in the URL , so it won’t extract the data . You can use your API url in the above script.

After running the above script in Python you will get the file generated in json format at your specified location.

{"status": {"code": 200, "message": "Success."}, 
"results": [{"symbol": "AAPL", "exchange": "BATS", "name": "Apple Inc", "dayCode": "9",
 "serverTimestamp": "2020-06-09T09:40:07-05:00", "mode": "i", "lastPrice": 337.98, 
"tradeTimestamp": "2020-06-09T10:25:07-05:00", "netChange": 4.520000000000039,
 "percentChange": 1.36, "unitCode": "2", "open": 331.64, "high": 338.04, "low": 331.12,
 "close": null, "flag": "", "volume": 735239}]}