Skip to content

Commit b02326d

Browse files
Export to JSON
1 parent b25e1d7 commit b02326d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

0x15-api/2-export_to_JSON.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/python3
2+
"""import api from a url"""
3+
import json
4+
import requests
5+
import sys
6+
7+
if __name__ == "__main__":
8+
try:
9+
id = int(sys.argv[1])
10+
url = "https://jsonplaceholder.typicode.com"
11+
res = requests.get('{}/users/{}'.format(url, id)).json()
12+
res_todo = requests.get("{}/todos".format(url),
13+
params={"userId": id}).json()
14+
name = res.get("username")
15+
user_data = list(map(
16+
lambda x: {
17+
"task": x.get("title"),
18+
"completed": x.get("completed"),
19+
"username": name
20+
},
21+
res_todo
22+
))
23+
with open('{}.json'.format(id), 'w') as apidata:
24+
data = {
25+
"{}".format(id): user_data
26+
}
27+
json.dump(data, apidata)
28+
except ValueError:
29+
pass

0 commit comments

Comments
 (0)