python - Filter JSON with null POST query return in API
问题描述
I have a python script that connects to a graphql API and sends a POST, so returning me a JSON this API belongs to a kanban, where it has cards arranged by its workflow, my problem and I have 152 cards, and this query is me returning 143, because some cards are not assigned to people, only exist in the workflow.
This generates an index error for the so-called 'assignees': row ['assignees'][0]['fullname']
, I put try/except
with index filter, and made him add this line again to the dictionary giving the full name Null
, however this seems to have failed because the total number of cards is 153 and not 143. Is there a more elegant way to do this data engineering?
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
data example:
{
"identifier": "x5g",
"title": "card name",
"description": "hello world",
"assignees": {"fullname": "John"},
"createdAt": "2020-04-04"
}
What generates error::
{
"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.
推荐阅读
- django - 如何使用 Django 通道将新创建的数据推送到客户端而不重新加载并且不完全改变现有的普通视图代码
- python-3.x - 如何在 django 模型中添加 3 个具有 3 个不同价格的不同许可证
- r - 因素 ggplot2 和颜色的多条趋势线
- c# - CS0122 Form1.AvgWaiting 由于其保护级别和 C# Windows 窗体中的甘特图而无法访问
- javascript - 在 wordpress 页面中编号有问题
- nginx - Ansible:没有与“nginx”匹配的包可用
- mongoose - 如何将 graphql-compose-mongoose 与 next.js apollo 服务器集成
- reactjs - 带有钩子的 React.js 中的 Formik 'initialValues' Typescript 错误
- angular - 这个错误的原因是什么?我该如何解决这个问题?
- php - 如何检查 Laravel Passport 中的访问令牌是否过期?