使用jq 1.5

时间:2019-09-06 09:14:34

标签: json jq

我已经能够将json对象(保存在名为auth.json的文件中)添加到Main.json文件中。 Auth.json

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

Main.json

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

使用以下命令,我已经能够将其添加到以下路径.paths.<any method that starts with />.get.parameters中。此外,仅当parameter数组尚未包含“ Authorization”对象时才添加它。 工作命令:

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

但是现在我必须将其添加到.paths.<any method that starts with />.<any value>.parameters内的每个对象下。

对于下面的输入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",
              }
            ]
            }
            }
            }
            }

预期输出应为:

{
      "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",
              }
            ]
            }
            }
            }
            }

所以我尝试了以下命令。

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

但是我得到了错误

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

我尝试使用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

但是我得到这个错误:

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

我还必须能够使用"name": "Authorization"删除任何对象(如果存在)并将其替换为auth.json的内容。

任何线索都将有所帮助。

所以预期的输出如下:

1 个答案:

答案 0 :(得分:1)

您的要求对我来说并不完全清楚,但是您程序的以下变体确实与问题描述的一种解释相符,并说明了如何使用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 )

这是一个与您对删除“授权”条目的第二组要求的一种解释相匹配的程序:

.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)