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: datastore.py ).
  • Now lets start importing the modules:
urllib.request
import json
  • urllib.request is 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
These are few free data
  • 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 , which can be either a string or a Request object.
  • 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
  • Create a empty json file in you dir (Eg: data.json ).
  • Now we need to open the file and write the data into it.
file = open("data.json","w")
file.write(data)
  • 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.
  • 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.
  • 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.

Follow me in Github, Twitter, Linkedin, Medium.

Full Stack Developer [Web | Mobile]