Retrieving data from api and storing in Json file (Python)
A simple project to store data from api in json file.
- Create a empty python file and name as you wish (Eg:
- Now lets start importing the modules:
urllib.requestis a Python module for fetching URLs (Uniform Resource Locators) or it defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more.
- Now search for any free api to reterive data and here are few free api.
- JSONPlaceholder search for resources and you will find different few data
- News API to get the news details
- Weather API — OpenWeatherMap you will get Weather data
- API Marketplace — Free Public & Open Rest APIs | RapidAPI other free links
- Get the api url for retrieving the data and paste it in any variable.
url = ('http://newsapi.org/v2/top-headlines?country=us&apiKey=***API-KEY***')
- Now request get from api url using
urlopen()this is to open the URL url, which can be either a string or a
response = urllib.request.urlopen(url)
- Now we need to read the data and decode it into
UTF-8(UTF-8 is a variable-width character encoding used for electronic communication.)
data = response.read().decode('UTF-8')
- Create a empty json file in you dir (Eg:
- Now we need to open the file and write the data into it.
file = open("data.json","w")
- open() returns a file object, write() is to write the data into it.
"w"- Write - will overwrite any existing content
- When you open json file you will see the data.
When you want to store the specific data.
- Now after the decoded the response, load the data in another variable using
json.loads()this is used convert str data to object.
new_data = json.loads(data)
get_articles = new_data['articles]
- After getting the required data convert it into str format. To convert we use
json.dumps()used to convert object to str.
new_data_articles = json.dumps(get_articles)
- Follow the previous step to open and write the data in the file.
Read data from json file convert into object
- Open the file and read the data and convert to object.
f = open("data.json", "r")
new_data = json.loads(f.read())
"r"- Read- reads every thing in the file.
- Now you can play with data.