简体   繁体   English

使用jq 1.5将文件中的对象与通配符一起替换到main.json中

[英]Replace object from a file into the main.json along with wildcards using jq 1.5

I have been able to add a json object - that is saved in a file named auth.json - into the Main.json file. 我已经能够将json对象添加到Main.json文件中,该对象保存在名为auth.json的文件中。 Auth.json Auth.json

{ 
    "name": "Authorization",
    "description": "this is authorization",
     "in": "header",
}

Main.json Main.json

{
  "swagger": "2.0",
    "paths": {
    "/agents/delta": {
      "get": {
        "description": "lorem ipsum doram",
        "parameters": [
          {
            "name": "since",
            "in": "query",
          }
        ]
        }
        }
        }
        }

Using the below command, I have been able to add it in the below path .paths.<any method that starts with />.get.parameters . 使用以下命令,我已经能够将其添加到以下路径.paths.<any method that starts with />.get.parameters Also it is getting added only if the parameters array does not already contain the 'Authorization' object. 此外,仅当parameter数组尚未包含“ Authorization”对象时才添加它。 Working command: 工作命令:

jq --slurpfile auth auth.json ' .paths |= with_entries(  if (.key|startswith("/")) and (.value.get.parameters[].name != "Authorization") then  .value.get.parameters += $auth  else  . end )' test.json > test1.json

But now I have to added it under every object within the .paths.<any method that starts with />.<any value>.parameters . 但是现在我必须将它添加到.paths.<any method that starts with />.<any value>.parameters每个对象下。

So for a input json like below. 因此对于如下所示的输入json。

{
      "swagger": "2.0",
        "paths": {
        "/agents/delta": {
          "get": {
            "description": "lorem ipsum doram",
            "parameters": [
              {
                "name": "since",
                "in": "query",
              }
            ]
            }
            "put": {
                        "description": "lorem ipsum doram",
            "parameters": [
              {
                "name": "since",
                "in": "query",
              }
            ]
            }
            }
            }
            }

expected output should be like: 预期输出应为:

{
      "swagger": "2.0",
        "paths": {
        "/agents/delta": {
          "get": {
            "description": "lorem ipsum doram",
            "parameters": [
              {
                "name": "since",
                "in": "query",
              },
              { 
                "name": "Authorization",
                "description": "this is authorization",
                "in": "header",
              }
            ]
            }
            "put": {
                        "description": "lorem ipsum doram",
            "parameters": [
              {
                "name": "since",
                "in": "query",
              },
              { 
                "name": "Authorization",
                "description": "this is authorization",
                "in": "header",
              }
            ]
            }
            }
            }
            }

So I tried the below command. 所以我尝试了以下命令。

jq --slurpfile auth auth.json ' .paths |= with_entries(  if (.key|startswith("/")) and (.value | .. | .parameters[]?.name != "Authorization") then  .value| .. |.parameters? += $auth  else  . end )' test.json

But I get the error 但是我得到了错误

jq: error (at test.json:3027): object ({"name":"si...) and array ([{"name":"A...) cannot be added

I tried using inside like the below 我尝试如下使用inside

jq --slurpfile auth auth.json '.paths |= with_entries(  if (.key|startswith("/")) and (.value | inside (["get","put","post"]) |.parameters[].name != "Authorization") then  .value.get.parameters += $auth  else  . end )' test.json

But I get this error: 但是我得到这个错误:

jq: error (at test.json:3027): array (["get","put...) and object ({"get":{"de...) cannot have their containment checked

I also have to be able to remove any object with "name": "Authorization" if it exists and replace it with the contents of auth.json. 我还必须能够删除带有"name": "Authorization"任何对象(如果存在)并将其替换为auth.json的内容。

Any leads will be helpful. 任何线索都将有所帮助。

So the expected output is as below: 因此,预期输出如下:

Your requirements are not entirely clear to me, but the following variant of your program does match one interpretation of your description of the problem and illustrates how to use map_values and all/2 : 您的要求对我来说并不完全清楚,但是您程序的以下变体确实符合问题描述的一种解释,并说明了如何使用map_valuesall/2

.paths |= with_entries(
  if (.key|startswith("/"))
  then .value |= map_values(
    if has("parameters") and all(.parameters[]; .name != "Authorization")
    then .parameters += $auth
    else . end ) 
  else . end )

And here's a program that matches one interpretation of your second set of requirements regarding removal of "Authorization" entries: 这是一个与您对删除“授权”条目的第二组要求的一种解释相匹配的程序:

.paths |= with_entries(
  if (.key|startswith("/"))
  then .value |= map_values(
    if has("parameters")
    then
    # first delete and then add
      .parameters |= map(select( .name != "Authorization"))
      | .parameters += $auth
    else  . end )
  else . end)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM