Retrieving data from api and storing in Json file (Python)

Aravind Kumar Vemula
3 min readJan 11, 2021

A simple project to store data from api in json file.

Get Started..

  • Create a empty python file and name as you wish (Eg: datastore.py ).
  • Now lets start importing the modules:
import 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 url, which can be either a string or a Request object.
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: 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.

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.

Thanks for Reading the Article!

Enjoy trying with new things

Follow me in Github, Twitter, Linkedin, Medium.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Aravind Kumar Vemula
Aravind Kumar Vemula

Written by Aravind Kumar Vemula

Full Stack Developer [Web | Mobile]

No responses yet

Write a response