首页 > 解决方案 > 在另一个 json 中替换和添加 json

问题描述

我有一个主要的 json 文件。

{
  "swagger": "2.0",
    "paths": {
    "/agents/delta": {
      "get": {
        "description": "lorem ipsum doram",
        "operationId": "getagentdelta",
        "summary": "GetAgentDelta",
        "tags": [
          "Agents"
        ],
        "parameters": [
          {
            "name": "since",
            "in": "query",
            "description": "Format - date-time (as date-time in RFC3339). The time from which you need changes from. You should use the format emitted by Date's toJSON method (for example, 2017-04-23T18:25:43.511Z). If a timestamp older than a week is passed, a business rule violation will be thrown which will require the client to change the from date. As a best-practice, for a subsequent call to this method, send the timestamp when you <b>started</b> the previous delta call (instead of when you completed processing the response or the max of the lastUpdateOn timestamps of the returned records). This will ensure that you do not miss any changes that occurred while you are processing the response from this method",
            "required": true,
            "type": "string"
          }
        ]
        }
        }
        }
        }

而且我有一个较小的 json 文件。

{ 
                                                                  "name": "Authorization",
                  "description": "This parameter represents the Authorization token obtained from the OKTA Authorization server. It is the Bearer token provided to authorize the consumer. Usage Authorization : Bearer token",
                  "in": "header",
                  "required": true,
                  "type": "string"
               }

现在我需要将较小的 json 文件的内容添加到参数数组中的 Main.Json 文件中。

我尝试了以下命令

cat test.json | jq --argfile sub Sub.json '.paths./agents/delta.get.parameters[ ] += $sub.{}' > test1.json

但我收到以下错误:

jq: error: syntax error, unexpected '{', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.paths += $sub.{}               
jq: 1 compile error
cat: write error: Broken pipe

我试过这个命令。

cat test.json | jq '.paths./agents/delta.get.parameters[ ] | = (.+ [{ "name": "Authorization", "description": "This parameter represents the Authorization token obtained from the OKTA Authorization server. It is the Bearer token provided to authorize the consumer. Usage Authorization : Bearer token", "in": "header",  "required": true,  "type": "string" }] )' > test1.json

而且我没有错误,也没有输出。我该如何解决这个问题?我必须先直接添加较小的 json 文件的内容。然后在稍后的阶段,搜索它是否已经有name: Authorization和它的其他参数,然后name: Authorization在以'/xx/yyy'开头的每个路径下删除并用smaller.json的实际内容替换整个片段。

编辑添加:对于问题的最后一部分,我无法使用 walk 功能,因为我有 jq 1.5 并且因为我在 Azure DevOps 中使用 bash 任务,所以我无法使用 walk 功能更新 jq 安装文件。同时我发现在 jq 中使用了类似于通配符的东西,并且想知道为什么我不能以这种方式使用它。

jq --slurpfile newval auth.json '.paths | .. | objects | .get.parameters += $newval' test.json > test1.json

谁能指出上述命令中的问题?它没有用,不知道为什么..

标签: jsonbashjq

解决方案


你想要--slurpfile,并且你需要/agents/delta用引号转义部分路径:

$ jq --slurpfile newval insert.json '.paths."/agents/delta".get.parameters += $newval' main.json
{
  "swagger": "2.0",
  "paths": {
    "/agents/delta": {
      "get": {
        "description": "lorem ipsum doram",
        "operationId": "getagentdelta",
        "summary": "GetAgentDelta",
        "tags": [
          "Agents"
        ],
        "parameters": [
          {
            "name": "since",
            "in": "query",
            "description": "Format - date-time (as date-time in RFC3339). The time from which you need changes from. You should use the format emitted by Date's toJSON method (for example, 2017-04-23T18:25:43.511Z). If a timestamp older than a week is passed, a business rule violation will be thrown which will require the client to change the from date. As a best-practice, for a subsequent call to this method, send the timestamp when you <b>started</b> the previous delta call (instead of when you completed processing the response or the max of the lastUpdateOn timestamps of the returned records). This will ensure that you do not miss any changes that occurred while you are processing the response from this method",
            "required": true,
            "type": "string"
          },
          {
            "name": "Authorization",
            "description": "This parameter represents the Authorization token obtained from the OKTA Authorization server. It is the Bearer token provided to authorize the consumer. Usage Authorization : Bearer token",
            "in": "header",
            "required": true,
            "type": "string"
          }
        ]
      }
    }
  }
}

这是一个在将新的 Authorization 对象插入每个参数数组之前首先从参数中删除任何现有 Authorization 对象的方法,并且不依赖于确切的路径:

jq --slurpfile newval add.json '.paths |= walk(
  if type == "object" and has("parameters") then
     .parameters |= map(select(.name != "Authorization")) + $newval
  else
     .
  end)' main.json

推荐阅读