我正在尝试将每个 context
值中的 #
更改为 %23
,但我遇到了空值问题。
缩短的 JSON 是:
{
"stats": {
"suites": 1
},
"results": [
{
"uuid": "676-a46b-47a1-a49f-4da4e46c1120",
"title": "",
"suites": [
{
"uuid": "gghjh-56a9-4713-b139-0d5b36bc7fbc",
"title": "Login process",
"tests": [
{
"pass": false,
"fail": true,
"pending": false,
"context": "\"screenshots/login.spec.js/Login process -- should login #11 (failed).png\""
},
{
"pass": false,
"fail": false,
"pending": true,
"context": null
}
]
}
]
}
]
}
我认为最接近正确的 JQ 命令是:
jq '.results[].suites[].tests[].context | strings | sub("#";"%23")'
但问题是我需要返回完整的编辑文件。我怎样才能做到这一点?
最佳答案
你很接近。要保留原始结构,您需要使用 update operator (|=
)而不是管道。将整个表达式括在它左边的括号中也是必要的,否则原始输入将对 |=
不可见。
(.results[].suites[].tests[].context | strings) |= sub("#"; "%23")
关于json - 在 JQ 中省略 sub() 的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65490789/