【发布时间】:2018-07-20 10:48:28
【问题描述】:
我正在尝试使用 jq 在 API 请求或响应的各个级别修改 json 数据,以支持 API 版本控制。
这是我的(简化的)测试 JSON:
[
{
"note": null,
"patient_id": 1,
"phenotypes": [
{
"name": "Breast carcinoma",
"observation": "present",
"patient_id": 1,
"person_id": 1
},
{
"name": "Breast carcinoma",
"observation": "present",
"patient_id": 1,
"person_id": 1
}
]
},
{
"note": null,
"patient_id": 2
},
{
"note": null,
"patient_id": 3,
"phenotypes": [
{
"name": "Breast carcinoma",
"observation": "present",
"patient_id": 3,
"person_id": 3
},
{
"name": "Breast carcinoma",
"observation": "present",
"patient_id": 3,
"person_id": 3
}
]
}
]
我有一个对象数组。每个对象可能有"phenotypes",我需要修改它的内容,以及从顶级对象中删除"note"。
目前我的jq如下:
[ map(del(.note, .age)) | .[] | select(.phenotypes != null) | .phenotypes |= map(del(.person_id)) ]
这几乎可行,但由于select(.phenotypes != null),数组中的第二个对象在过滤后永远不会返回。
我也尝试过使用 if-then-else (end),但是我不能让它不出错,而且我找不到任何示例或文档表明它可以用于进一步表达。
我的预期输出如下:
[
{
"patient_id": 1,
"phenotypes": [
{
"name": "Breast carcinoma",
"observation": "present",
"patient_id": 1
},
{
"name": "Breast carcinoma",
"observation": "present",
"patient_id": 1
}
]
},
{
"patient_id": 2
},
{
"patient_id": 3,
"phenotypes": [
{
"name": "Breast carcinoma",
"observation": "present",
"patient_id": 3
},
{
"name": "Breast carcinoma",
"observation": "present",
"patient_id": 3
}
]
}
]
note 已从根目录中删除。
person_id 已从 phenotypes 中删除。
【问题讨论】:
-
我已经包含了预期的输出。谢谢。