jq - 如何将函数应用于jq中的多个字段
问题描述
我尝试减小 jq 调用的大小。当前命令是:
jq '.result | .[].newValueJson.action |= (. | tostring // .) | .[].oldValueJson.action |= (. | tostring // .) | .[].metadata.value |= (. | tostring // .)'
如您所见,函数“tostring”以相同的方式应用于“newValueJson.action”、“oldValueJson.action”和“metadata.value”。我想知道是否有更紧凑的语法,所以我只需要应用 tostring 一次?
我提取的样本数据显示了正在做什么(这不是完整的 json 树)。
资源:
{
"result": [{
"id": 1,
"action": {
"result": true,
"type": "filter_create"
},
"newValueJson": {
"action": "simulate"
},
"oldValueJson": {
"action": "enforce"
},
"metadata": {
"value": false
}
},
{
"id": 2,
"action": {
"result": true,
"type": "filter_create"
},
"newValueJson": {
"action": {
"mode": "simulate",
"timeout": 3600
}
},
"oldValueJson": {
"action": {
"mode": "enforce",
"timeout": 3600
}
},
"metadata": {
"value": "off"
}
}
]
}
结果:
[{
"id": 1,
"action": {
"result": true,
"type": "filter_create"
},
"newValueJson": {
"action": "simulate"
},
"oldValueJson": {
"action": "enforce"
},
"metadata": {
"value": "false"
}
},
{
"id": 2,
"action": {
"result": true,
"type": "filter_create"
},
"newValueJson": {
"action": "{\"mode\":\"simulate\",\"timeout\":3600}"
},
"oldValueJson": {
"action": "{\"mode\":\"enforce\",\"timeout\":3600}"
},
"metadata": {
"value": "off"
}
}
]
谢谢和最好的。
解决方案
您可以使用(..)
如下方式将操作分组在一起。此外.[]
,您可以使用数组表示法map(..)
来处理应用过滤器表达式,而不是使用result
数组表示法
.result | map((.newValueJson.action, .oldValueJson.action, .metadata.value ) |= (. | tostring // .))
推荐阅读
- loops - 在循环中批量计数字符
- java - 如何在一定时间内启动 Andorid Wifi Scan?
- javascript - *ngIf 更改未在模板中检测到
- sql - 新的 SQL 学习者,使用 SQL Server Mang。工作室。我收到错误消息,消息 2714,级别 16,状态 6,第 3 行已经有一个名为
- python - 使用 Python 或 pyspark 读取基于位置的 CSV 文件
- android - 为什么我下载依赖项时我的 Flutter 应用程序无法编译?
- node.js - 如何使用 Firestore 从 DialogFlow fullfilment 上的 snap 中获取地图数组
- javascript - https 在 javascript 中发布类似于带有标头和参数的 curl -d 带有“请求”模块 nodejs npm
- javascript - 我有 google drive pdf 的共享链接,我必须使用 angular7 下载该 pdf。请告诉我该怎么做
- material-ui - 如何为material-ui-dropzone的容器设置样式