jq:基于对象值条件递归删除对象的最简单方法

时间:2015-12-04 21:45:46

标签: json jq

我想使用jq删除JSON“对象”中的所有字典(我使用该术语通常是指数组或字典)

a)包含一个名为“delete_me”的键,AND b)键“delete_me”满足某个预定条件(null,非零,true等)

基本上,我想要实现的逻辑是:遍历输入,并在每个节点,如果该节点不是数组或对象,然后保留它并继续前进,否则,保留它但从中移除任何子节点这是条件a)或b)失败的字典。

有什么建议吗?

示例输入:

{
  "a": { "foo": "bar" },
  "b": {
    "i": {
      "A": {
        "i": [
          {
            "foo": {},
            "bar": {
              "delete_if_this_is_null": false,
              "an_array": [],
              "another_array": [
                {
                    "delete_if_this_is_null": null,
                    "foo": "bar"
                }
              ],
              "etc": ""
            },
            "foo2": "s"
          },
          {
            "foo": {
              "an_array": [
                {
                  "delete_if_this_is_null": "ok",
                  "foo":"bar",
                  "another_object": { "a":1 }
                },
                {
                  "delete_if_this_is_null": null,
                  "foo2":"bar2",
                  "another_object": { "a":1 },
                  "name": null
                }
              ],
              "an_object": {
                "delete_if_this_is_null":null,
                "foo3":"bar3"
              }
            },
            "zero": 0,
            "b": "b"
          }
        ]
      }
    }
  }
}
如果“delete_me”键为delete_if_this_is_null并且预定条件为delete_if_this_is_null == null

应该产生:

{
  "a": { "foo": "bar" },
  "b": {
    "i": {
      "A": {
        "i": [
          {
            "foo": {},
            "bar": {
              "delete_if_this_is_null": false,
              "an_array": [],
              "another_array": [],
              "etc": ""
            },
            "foo2": "s"
          },
          {
            "foo": {
              "an_array": [
                {
                  "delete_if_this_is_null": "ok",
                  "foo":"bar",
                  "another_object": { "a":1 }
                }
              ]
            },
            "zero": 0,
            "b": "b"
          }
        ]
      }
    }
  }
}

<小时/> 更新:这是解决方案:假设输入文件'input.json':

jq 'def walk(f):
  . as $in
  | if type == "object" then
      reduce keys[] as $key
        ( {}; . + { ($key):  ($in[$key] | walk(f)) } ) | f
  elif type == "array" then map( walk(f) ) | f
  else f
  end;

def mapper(f):
  if type == "array" then map(f)
  elif type == "object" then
  . as $in
  | reduce keys[] as $key
      ({};
       [$in[$key] | f ] as $value
       | if $value | length == 0 then .
         else . + {($key): $value[0]} end)
  else .
  end;

walk( mapper(select((type == "object" and .delete_if_this_is_null == null) | not)) )' < input.json

3 个答案:

答案 0 :(得分:2)

这是一个使用递归函数的解决方案:

def clean(condition):
  if type == "object" then
    if condition
    then empty
    else
    with_entries(
      if (.value|type) == "object" and (.value|condition)
      then empty
      else .value |= clean(condition)
      end
    )
    end
  elif type == "array" then
    map(
      if type == "object" and condition
      then empty
      else clean(condition)
      end
    )
  else .
  end
;

clean(
  has("delete_if_this_is_null") and (.delete_if_this_is_null == null)
)

答案 1 :(得分:1)

我不确定您在问题中想要完成的是什么,但我假设您想要递归搜索json响应并删除满足某些条件的json对象。

您可以在walk过滤器的帮助下轻松完成此操作,该过滤器将在未来版本的jq中出现,请参阅source中的实现。

# Apply f to composite entities recursively, and to atoms
def walk(f):
  . as $in
  | if type == "object" then
      reduce keys[] as $key
        ( {}; . + { ($key):  ($in[$key] | walk(f)) } ) | f
  elif type == "array" then map( walk(f) ) | f
  else f
  end;

有了这个,您可以像这样过滤掉它们:

def filter_objects(predicate): # removes objects that satisfies some predicate
    walk(
        if (type == "object") and (predicate) then
            empty
        else
            .
        end
    )
    ;
filter_objects(.delete_me) # remove objects that has a truthy property "delete_me"

答案 2 :(得分:1)

杰夫的解决方案可能会过多。例如,使用:

def data: [1,2, {"hello": {"delete_me": true, "a":3 }, "there": 4} ]; ];

杰夫的解决方案产生空(即没有)。

因此,以下内容可能更接近您所寻找的内容:

walk(if (type == "object" and .delete_me) then del(.) else . end )

对于data,这会产生:

[1,2,{"hello":null,"there":4}]

替代解决方案

如果需要在上面的示例中消除"hello":null的解决方案,则需要jq的map_values / 1的变体。这是一种方法:

def mapper(f):
  if type == "array" then map(f)
  elif type == "object" then
  . as $in
  | reduce keys[] as $key
      ({};
       [$in[$key] | f ] as $value
       | if $value | length == 0 then . 
         else . + {($key): $value[0]} end)
  else .
  end;

data | walk( mapper(select((type == "object" and .delete_me) | not)) )

结果是:

[1,2,{"there":4}]
相关问题