在 Python 中规范化 Monday.com API JSON 输出
Normalize Monday.com API JSON output in Python
我最终试图从 Monday.com API 请求的 JSON 输出生成一个 csv。
下面是我目前的代码。我在尝试将 JSON 展平为 table.
时遇到问题
import requests
import json
import pandas as pd
apiKey = "API Key Here"
apiUrl = "https://api.monday.com/v2"
headers = {"Authorization" : apiKey}
query2 = '{boards(ids:123456) {items{name, column_values{title text } } } }'
data = {'query' : query2}
json_data = json.loads(requests.post(url=apiUrl, json=data, headers=headers).text)
norm=pd.json_normalize(json_data, 'items',['data', 'boards'])
JSON 从 API 输出。为了便于阅读,我添加了一些换行符。
{'data':
{'boards':
[{'items':
[{'name': 'Item 1', 'column_values': [{'title': 'Person', 'text': 'Mark McCoy'}, {'title': 'Status', 'text': None}, {'title': 'Date', 'text': '2021-02-05'}]},
{'name': 'This is a new item', 'column_values': [{'title': 'Person', 'text': ''}, {'title': 'Status', 'text': None}, {'title': 'Date', 'text': '2021-04-17'}]},
{'name': 'Item 5', 'column_values': [{'title': 'Person', 'text': ''}, {'title': 'Status', 'text': None}, {'title': 'Date', 'text': '2021-02-13'}]},
{'name': 'Item 2', 'column_values': [{'title': 'Person', 'text': ''}, {'title': 'Status', 'text': 'Done'}, {'title': 'Date', 'text': '2021-05-14'}]}]}]},
'account_id': 00000000}
当我 运行 终端中的 ,py 文件时,我得到以下输出
Traceback (most recent call last):
File "/Users/markamccoy/Desktop/MondayPy/stack.py", line 14, in <module>
norm=pd.json_normalize(json_data, 'items',['data', 'boards'])
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/json/_normalize.py", line 336, in _json_normalize
_recursive_extract(data, record_path, {}, level=0)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/json/_normalize.py", line 309, in _recursive_extract
recs = _pull_records(obj, path[0])
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/json/_normalize.py", line 248, in _pull_records
result = _pull_field(js, spec)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/json/_normalize.py", line 239, in _pull_field
result = result[spec]
KeyError: 'items'
我完全是 python 菜鸟,阅读 pandas 文档让我走到这一步,但我有点不适应。
如果您使用 normalize.the,结果将如下所示。
df = pd.json_normalize(json_data['data']['boards'][0]['items'],record_path='column_values',meta=['name'])
title text name
0 Person Mark McCoy Item 1
1 Status None Item 1
2 Date 2021-02-05 Item 1
3 Person This is a new item
4 Status None This is a new item
5 Date 2021-04-17 This is a new item
但我认为不是你 want.and json_normalize 中没有示例来平整那种数组。
data = [ [item['name']]+[c_v['text'] for c_v in item['column_values']] for item in json_data['data']['boards'][0]['items']]
df = pd.DataFrame(data,columns=['name','Person','Status','Date'])
name Person Status Date
0 Item 1 Mark McCoy None 2021-02-05
1 This is a new item None 2021-04-17
2 Item 5 None 2021-02-13
3 Item 2 Done 2021-05-14
所以我只能在python.
平铺
如果你有进一步的question.please评论给我
我最终试图从 Monday.com API 请求的 JSON 输出生成一个 csv。
下面是我目前的代码。我在尝试将 JSON 展平为 table.
时遇到问题import requests
import json
import pandas as pd
apiKey = "API Key Here"
apiUrl = "https://api.monday.com/v2"
headers = {"Authorization" : apiKey}
query2 = '{boards(ids:123456) {items{name, column_values{title text } } } }'
data = {'query' : query2}
json_data = json.loads(requests.post(url=apiUrl, json=data, headers=headers).text)
norm=pd.json_normalize(json_data, 'items',['data', 'boards'])
JSON 从 API 输出。为了便于阅读,我添加了一些换行符。
{'data':
{'boards':
[{'items':
[{'name': 'Item 1', 'column_values': [{'title': 'Person', 'text': 'Mark McCoy'}, {'title': 'Status', 'text': None}, {'title': 'Date', 'text': '2021-02-05'}]},
{'name': 'This is a new item', 'column_values': [{'title': 'Person', 'text': ''}, {'title': 'Status', 'text': None}, {'title': 'Date', 'text': '2021-04-17'}]},
{'name': 'Item 5', 'column_values': [{'title': 'Person', 'text': ''}, {'title': 'Status', 'text': None}, {'title': 'Date', 'text': '2021-02-13'}]},
{'name': 'Item 2', 'column_values': [{'title': 'Person', 'text': ''}, {'title': 'Status', 'text': 'Done'}, {'title': 'Date', 'text': '2021-05-14'}]}]}]},
'account_id': 00000000}
当我 运行 终端中的 ,py 文件时,我得到以下输出
Traceback (most recent call last):
File "/Users/markamccoy/Desktop/MondayPy/stack.py", line 14, in <module>
norm=pd.json_normalize(json_data, 'items',['data', 'boards'])
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/json/_normalize.py", line 336, in _json_normalize
_recursive_extract(data, record_path, {}, level=0)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/json/_normalize.py", line 309, in _recursive_extract
recs = _pull_records(obj, path[0])
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/json/_normalize.py", line 248, in _pull_records
result = _pull_field(js, spec)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/json/_normalize.py", line 239, in _pull_field
result = result[spec]
KeyError: 'items'
我完全是 python 菜鸟,阅读 pandas 文档让我走到这一步,但我有点不适应。
如果您使用 normalize.the,结果将如下所示。
df = pd.json_normalize(json_data['data']['boards'][0]['items'],record_path='column_values',meta=['name'])
title text name
0 Person Mark McCoy Item 1
1 Status None Item 1
2 Date 2021-02-05 Item 1
3 Person This is a new item
4 Status None This is a new item
5 Date 2021-04-17 This is a new item
但我认为不是你 want.and json_normalize 中没有示例来平整那种数组。
data = [ [item['name']]+[c_v['text'] for c_v in item['column_values']] for item in json_data['data']['boards'][0]['items']]
df = pd.DataFrame(data,columns=['name','Person','Status','Date'])
name Person Status Date
0 Item 1 Mark McCoy None 2021-02-05
1 This is a new item None 2021-04-17
2 Item 5 None 2021-02-13
3 Item 2 Done 2021-05-14
所以我只能在python.
平铺
如果你有进一步的question.please评论给我