首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于重写URL的jq代码,其中一些值是数组和一些字符串

用于重写URL的jq代码,其中一些值是数组和一些字符串
EN

Stack Overflow用户
提问于 2021-03-20 22:56:13
回答 2查看 38关注 0票数 0

在下面的json中,我需要重写configURL的值,并在https://后面(也是唯一的)添加staging. -忽略'http://'‘。问题是configURL键存储的一些值是数组。原始json看起来是这样的:

代码语言:javascript
复制
{
    "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"
        ]
    }
}

所需的输出为

代码语言:javascript
复制
{
    "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而只有一个的情况下才有效;我不知道如何为添加条件,以及如何修改数组中的每个值。

代码语言:javascript
复制
jq -r 'def camel:gsub( "https://"; "https://staging."); walk( if type=="object" and .configURL then (.configURL |= camel) else . end)' apps.json
EN

回答 2

Stack Overflow用户

发布于 2021-03-20 23:04:25

您已经检查了一个对象的type一次--只需再检查一次:

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2021-03-22 17:44:21

另一种方法是使用(.[]? // .) -它本质上是一个if/else - ?捕获错误,// .返回原始值。

代码语言:javascript
复制
$ 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

然后可以直接转换结果。

代码语言:javascript
复制
jq -r '(.[].configURL | (.[]? // .)) |= sub("^https://"; "https://staging.")'

您的configURL键都在同一位置,因此不需要递归/遍历,但如果需要,则需要使用select()过滤掉可能的空值

代码语言:javascript
复制
jq -r '(.. | .configURL? | select(.) | (.[]? // .)) |= sub("^https://"; "https://staging.")'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66723014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档