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

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

  • Now lets start importing the modules:
import urllib.request
import json
  • 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
url = ('http://newsapi.org/v2/top-headlines?country=us&apiKey=***API-KEY***')
response = urllib.request.urlopen(url)
data = response.read().decode('UTF-8')
  • Now we need to open the file and write the data into it.
file = open("data.json","w")
file.write(data)
  • When you open json file you will see the data.
new_data = json.loads(data)
get_articles = new_data['articles]
new_data_articles = json.dumps(get_articles)
f = open("data.json", "r")
new_data = json.loads(f.read())
  • Now you can play with data.

Full Stack Developer [Web | Mobile]