【问题标题】:jq removing inner child node without breaking the jsonjq 在不破坏 json 的情况下删除内部子节点
【发布时间】:2018-03-07 03:54:31
【问题描述】:

我试图让 jq 从 json 文件中删除一个内部节点,json 文件如下所示:

    {
  "etag": "14b3796c268c87553291702c808e86dfe1e53d1b",
  "rules": {
    "name": "default",
    "children": [
      {
        "name": "xxxx",
        "children": [
          {
            "name": "dffaa42b-3f0f-425f-a9a1-a63cd35b2517",
            "children": [],
            "behaviors": [
              {
                "name": "xxx",
                "options": {
                  "key": "xxx-xxx-xxx-xxx",
                  "compress": true,
                  "ports": ""
                }
              }
            ],
            "criteria": [
              {
                "name": "xxxx",
                "options": {
                  "Name": "UUID",
                  "values": [
                    "dffaa42b-3f0f-425f-a9a1-a63cd35b2517"
                  ]
                }
              }
            ],
            "criteriaMustSatisfy": "all"
          },
          {
            "name": "7004389c-c47a-4611-9bd7-9f5dfe051d17",
            "children": [],
            "behaviors": [
              {
                "name": "xxx",
                "options": {
                  "key": "xxx-xxx-xxx-xxx",
                  "compress": true,
                  "ports": ""
                }
              }
            ],
            "criteria": [
              {
                "name": "xxxx",
                "options": {
                  "Name": "UUID",
                  "values": [
                    "7004389c-c47a-4611-9bd7-9f5dfe051d17"
                  ]
                }
              }
            ],
            "criteriaMustSatisfy": "all"
          }
        ],
        "behaviors": [],
        "criteria": [],
        "criteriaMustSatisfy": "all"
      }
    ],
    "behaviors": [
      {
        "name": "xxx",
        "options": {}
      }
    ],
    "options": {
      "is_secure": true
    },
    "variables": []
    },
  "warnings": [
  ],
  "Format": "xxx"
}

我可能混淆了json结构,但是我现在的jq查询如下:

(.rules.children[].children[] | select(.name | contains("7004389c-c47a-4611-9bd7-9f5dfe051d17")| not ))

这可行,但它返回的 json 不包括来自 .rules.children[].children[] 的子项。

我如何让 jq 返回整个 json 文件,不包括过滤器中识别的 json?

【问题讨论】:

  • 我忘了提,我基本上想返回相同的 json,除了:{“name”:“7004389c-c47a-4611-9bd7-9f5dfe051d17”,“children”:[],“behaviors ": [ { "name": "xxxx", } ], }
  • 您可以edit您的问题,而不是在cmets中添加信息。
  • @Dale,你的json无效,贴出实际的
  • @RomanPerekhrest 道歉,我已经修改以包含正确的 Json

标签: arrays json edit jq


【解决方案1】:

del/1 能够根据路径表达式删除节点。

这个例子似乎不是最小的,所以让我们考虑一下:

  {"a":{"c":[
         {"d":{"c":[{"e":"xyzzy"},{"e":2}]}},
         {"d":{"c":[{"e":"xyzzy"},{"e":2}]}} ]}}

现在假设我们要删除 .e == "xyzz" 的对象:

del( .a.c[].d.c[] | select(.e == "xyzzy") )

这将导致: {"a":{"c":[{"d":{"c":[{"e":2}]}},{"d":{"c":[{"e":2 }]}}]}}

很遗憾,jq 1.5 不支持del/1 中的复杂路径规范;然而,使用足够新的 jq 版本,我们可以这样写:

del( .a.c[].d.c[] | select(.e | type == "string" and contains("xyzzy") ) )

因此,根据上述关于 jq 1.5 的警告,您可以这样写:

del(.rules.children[].children[] | select(.name| contains("7004389c-c47a-4611-9bd7-9f5dfe051d17")))

jq 1.5 的解决方法

.rules.children[].children |= 
  map(select((.name? // "")
             | contains("7004389c-c47a-4611-9bd7-9f5dfe051d17")
             | not))

【讨论】:

  • 感谢Peak,工作就像一个魅力。我想知道为什么 del() 函数不起作用,现在我在 JQ1.5 上。
猜你喜欢
  • 2021-05-10
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
相关资源
最近更新 更多