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?
JavaScript
x
38
38
1
from flask import Flask, jsonify, abort, make_response
2
from flask_restful import Resource, Api
3
import requests, json
4
import urllib.request as request
5
6
app = Flask(__name__)
7
api = Api(app)
8
9
data = "given_URL"
10
myjsondata = {}
11
12
@app.route('/my_folder/', methods=["POST"])
13
@app.error
14
15
class read(Resource):
16
17
def read_data(self):
18
with request.urllib.urlopen(metadata) as response:
19
if response.getcode() == 200:
20
source = response.read()
21
data = json.loads(source)
22
for i in data:
23
for key, value in i.iteritems():
24
if key == 'uid':
25
print key, value
26
myjsondata[key]=value
27
28
return jsonify(myjsondata)
29
30
else:
31
abort(make_response(jsonify(404, massage = "No request found"),400))
32
33
api.add_resource(read, '/my_folder')
34
35
if __name__ == '__main__':
36
app.run(debug=True)
37
38
Advertisement
Answer
Read the metadata. Arrange keys and values in your way to make dictionary.
JavaScript
1
13
13
1
#Save extracted content to dictionary/list
2
myjsondata={}
3
for i in data:
4
for key, value in i.iteritems():
5
if key == 'uid':
6
print key, value
7
myjsondata[key]=value
8
9
#Save your dictionary content to json file
10
with open('saved_json_file_1.json', 'w') as saveFile:
11
saveFile.write(json.dumps())
12
13