Jq:为什么del()留空?

时间:2017-03-06 21:17:26

标签: jq

我必须递归删除包含给定键值的对象。

Json是:

{
    "health": "yellow",
    "status": "open",
    "index": ".monitoring-es-2-2017.03.03",
    "uuid": "Py3FY-UwSjmda3jbYeIdMg",
    "pri": "1",
    "rep": "1",
    "docs.count": "23494",
    "docs.deleted": "78",
    "store.size": "12mb",
    "pri.store.size": "12mb"
  },
  {
    "health": "yellow",
    "status": "open",
    "index": "robot-log-01.02.2017",
    "uuid": "lFndm8gDS2OAGXZhP5KSkw",
    "pri": "5",
    "rep": "1",
    "docs.count": "127887",
    "docs.deleted": "0",
    "store.size": "35.1mb",
    "pri.store.size": "35.1mb"
  }

配置是:

walk( if ((type == "object" and has("index")) and (."index" | test("monitoring*"))) then del(.) else . end )

输出是:

null,
      {
        "health": "yellow",
        "status": "open",
        "index": "robot-log-01.02.2017",
        "uuid": "lFndm8gDS2OAGXZhP5KSkw",
        "pri": "5",
        "rep": "1",
        "docs.count": "127887",
        "docs.deleted": "0",
        "store.size": "35.1mb",
        "pri.store.size": "35.1mb"
      }

为什么del()留空?如何避免呢?

1 个答案:

答案 0 :(得分:1)

简而言之,请使用empty代替del(.)。简化后:

walk(如果type ==“object”并且有(“index”)和(.index | test(“monitoring”))         然后清空其他。结束)