在API中使用空POST查询返回筛选JSON



我有一个python脚本,它连接到graphql API并发送POST,所以返回我一个JSON这个API属于一个看板,它有按工作流排列的卡片,我的问题和我有152张卡片,这个查询是我返回143,因为有些卡片没有分配给人,只存在于工作流中。这为所谓的'assignees': row ['assignees'][0]['fullname']生成了一个索引错误,我用索引过滤器放置了try/except,并让他再次将这一行添加到字典中,给出全名Null,但这似乎失败了,因为卡的总数是153,而不是143。有没有更优雅的方法来进行这种数据工程?

py:

dataDict = {}
for row in ModelData:
try:
dataDict.update(
{ row["identifier"]: 
{'title' : row["title"], 
'description' : row["description"], 
'assignees' : row['assignees'][0]['fullname'], 
'createdAt' : row["createdAt"]}})
except IndexError as e:
dataDict.update(
{ row["identifier"]:
{'title' : row["title"], 
'description' : row["description"], 
'assignees' : "", 
'createdAt' : row["createdAt"]}})
pass

数据示例:

{
"identifier": "x5g",
"title": "card name",
"description": "hello world",
"assignees": {"fullname": "John"},
"createdAt": "2020-04-04"
}

产生错误的原因::

{
"identifier": "x6g",
"title": "card name 2",
"description": "hello world",
"assignees": {"fullname": ""},
"createdAt": "2020-04-04"
}

您可以使用if语句:


dataDict.update(
{ 
row["identifier"]: {
'title' : row["title"], 
'description' : row["description"], 
'assignees' : row['assignees'][0]['fullname'] if 'assignees' in row else "", 
'createdAt' : row["createdAt"]
}
})
I dont know what the data you get looks like without the assignees but I think you get the Idea.

最新更新