jq删除内部子节点而不破坏json

时间:2018-03-07 03:54:31

标签: arrays json edit jq

我试图让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?

1 个答案:

答案 0 :(得分:0)

{"a":{"c":[ {"d":{"c":[{"e":"xyzzy"},{"e":2}]}}, {"d":{"c":[{"e":"xyzzy"},{"e":2}]}} ]}} 能够根据路径表达式删除节点。

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

.e == "xyzz"

现在假设我们要删除del( .a.c[].d.c[] | select(.e == "xyzzy") )

的对象
del/1

这会导致:     { “一”:{ “C”:[{ “d”:{ “C”:[{ “E”:2}]}},{ “d”:{ “C”:[{ “E”:2 }]}}]}}

不幸的是,jq 1.5不支持del( .a.c[].d.c[] | select(.e | type == "string" and contains("xyzzy") ) ) 中的复杂路径规范;但是,使用最新版本的jq,我们可以写:

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

jq 1.5

的解决方法
@IBAction func open_app(_ sender: Any) 
{ extensionContext?.open(URL(string: "open://")! , 
completionHandler: nil)
 }