I have defined a URL in my python codes and I need to read it and save it in a json file then save them in a list or dictionary. In my codes I read and print them. Also, I would like to know is there any issue in my codes such as post method or read key value pairs from the URL?
from flask import Flask, jsonify, abort, make_response
from flask_restful import Resource, Api
import requests, json
import urllib.request as request
app = Flask(__name__)
api = Api(app)
data = "given_URL"
myjsondata = {}
@app.route('/my_folder/', methods=["POST"])
@app.error
class read(Resource):
def read_data(self):
with request.urllib.urlopen(metadata) as response:
if response.getcode() == 200:
source = response.read()
data = json.loads(source)
for i in data:
for key, value in i.iteritems():
if key == 'uid':
print key, value
myjsondata[key]=value
return jsonify(myjsondata)
else:
abort(make_response(jsonify(404, massage = "No request found"),400))
api.add_resource(read, '/my_folder')
if __name__ == '__main__':
app.run(debug=True)
Advertisement
Answer
Read the metadata. Arrange keys and values in your way to make dictionary.
#Save extracted content to dictionary/list
myjsondata={}
for i in data:
for key, value in i.iteritems():
if key == 'uid':
print key, value
myjsondata[key]=value
#Save your dictionary content to json file
with open('saved_json_file_1.json', 'w') as saveFile:
saveFile.write(json.dumps())