-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql_noauth_activity.py
84 lines (77 loc) · 1.63 KB
/
graphql_noauth_activity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# %%
# https://towardsdatascience.com/connecting-to-a-graphql-api-using-python-246dda927840
import requests
import json
import jsonlines
import pandas as pd
import pyarrow as pa
import pyarrow.json as pj
# %%
query = """
query {
characters {
results {
name
status
species
type
gender
}
}
}
"""
query = """
query {
characters {
results {
name
status
species
type
gender
image
episode{
episode
name
air_date
}
}
}
}
"""
# %%
url = 'https://rickandmortyapi.com/graphql/'
r = requests.post(url, json={'query': query})
print(r.status_code)
print(r.text)
# %%
json_data = json.loads(r.text)
# %%
df_data = json_data['data']['characters']['results']
df = pd.json_normalize(df_data)
# %%
# https://blog.softhints.com/python-convert-json-to-json-lines/
with jsonlines.open('output.jsonl', mode='w') as writer:
writer.write_all(df_data)
# %%
datjl = pd.read_json("output.jsonl", lines=True)
# %%
pj.read_json("output.jsonl", parse_options = pj.ParseOptions(newlines_in_values=True))
# %%
# if we want to convert the format to JSON arrays.
# df_data = json_data['data']['characters']['results']
# for i, value in enumerate(df_data):
# print(i)
# idat = df_data[i].copy()
# number = list()
# name = list()
# airdate = list()
# for j, jvalue in enumerate(idat['episode']):
# iseason = idat['episode'][j]
# number.append(iseason['episode'])
# name.append(iseason['name'])
# airdate.append(iseason['air_date'])
# df_data[i]['episode'] = {
# 'number':number,
# "name":name,
# "air_date":airdate}