在下面的json中,我需要重写configURL的值,并在https://后面(也是唯一的)添加staging. -忽略'http://'‘。问题是configURL键存储的一些值是数组。原始json看起来是这样的:
{
"app1.json": {
"content": "app-config",
"updates": 2,
"configURL": "https://companyX.com/app1/config.json"
},
"app2.json": {
"content": "app-config",
"updates": 2,
"configURL": [
"https://some-company.com/app2/config.json",
"https://some-company.com/app2/config-resources.json",
"http://some-company.com/app2/config-resources.json"
]
},
"app3.ini": {
"content": "binaries",
"scope": "deploy",
"configURL": "https://staging.app4.com/app4/installs/binaries.ini",
"resources": [
"https://app4.com/resource",
"https://app4.com/resource.bin",
"https://app4.com/resource2.bin"
]
},
"app4.json": {
"content": "app-config",
"updates": 3,
"configURL": [
"https://different-company.com/app3/config.json",
"https://different-company.com/app3/config-resources.json",
"https://different-company.com/app3/config-binaries.json"
],
"resources": [
"https://different-company.com/resource",
"https://different-company.com/resource.bin",
"https://different-company.com/resource2.bin"
]
}
}所需的输出为
{
"app1.json": {
"content": "app-config",
"updates": 2,
"configURL": "https://staging.companyX.com/app1/config.json"
},
"app2.json": {
"content": "app-config",
"updates": 2,
"configURL": [
"https://staging.some-company.com/app2/config.json",
"https://staging.some-company.com/app2/config-resources.json",
]
},
"app3.ini": {
"content": "binaries",
"scope": "deploy",
"configURL": "https://staging.staging.app4.com/app4/installs/binaries.ini",
"resources": [
"https://app4.com/resource",
"https://app4.com/resource.bin",
"https://app4.com/resource2.bin"
]
},
"app4.json": {
"content": "app-config",
"updates": 3,
"configURL": [
"https://staging.different-company.com/app3/config.json",
"https://staging.different-company.com/app3/config-resources.json",
"https://staging.different-company.com/app3/config-binaries.json"
],
"resources": [
"https://different-company.com/resource",
"https://different-company.com/resource.bin",
"https://different-company.com/resource2.bin"
]
}
}我想出的以下解决方案只有在没有多个URL而只有一个的情况下才有效;我不知道如何为添加条件,以及如何修改数组中的每个值。
jq -r 'def camel:gsub( "https://"; "https://staging."); walk( if type=="object" and .configURL then (.configURL |= camel) else . end)' apps.json发布于 2021-03-20 23:04:25
您已经检查了一个对象的type一次--只需再检查一次:
jq -r '
def camel:
if type == "string" then
gsub( "https://"; "https://staging.")
else
.[] |= camel
end;
walk( if type=="object" and .configURL then (.configURL |= camel) else . end)
' apps.json发布于 2021-03-22 17:44:21
另一种方法是使用(.[]? // .) -它本质上是一个if/else - ?捕获错误,// .返回原始值。
$ jq -r '(.[].configURL | (.[]? // .))' apps.json
https://companyX.com/app1/config.json
https://some-company.com/app2/config.json
https://some-company.com/app2/config-resources.json
http://some-company.com/app2/config-resources.json
https://staging.app4.com/app4/installs/binaries.ini
https://different-company.com/app3/config.json
https://different-company.com/app3/config-resources.json
https://different-company.com/app3/config-binaries.json然后可以直接转换结果。
jq -r '(.[].configURL | (.[]? // .)) |= sub("^https://"; "https://staging.")'您的configURL键都在同一位置,因此不需要递归/遍历,但如果需要,则需要使用select()过滤掉可能的空值
jq -r '(.. | .configURL? | select(.) | (.[]? // .)) |= sub("^https://"; "https://staging.")'https://stackoverflow.com/questions/66723014
复制相似问题