你如何排除财产?

时间:2017-05-25 10:35:45

标签: json object key jq

我有以下数据集:

[
  {
    "py/object": "bit.ast.Node",
    "_children": [
      {
        "py/object": "bit.ast.Node",
        "_children": [
          "main",
          {
            "py/object": "bit.ast.Node",
            "_children": [
              "args",
              {
                "py/object": "bit.ast.Node",
                "_children": [
                  {
                    "py/object": "bit.ast.Node",
                    "_children": [
                      "str"
                    ],
                    "source_column": 2,
                    "source_filename": "tests/fixture/hello.b",
                    "source_line": 1,
                    "tag": "type-named"
                  }
                ],
                "base": {
                  "py/id": 10
                },
                "source_column": 2,
                "source_filename": "tests/fixture/hello.b",
                "source_line": 1,
                "tag": "type",
                "type": "array"
              }
            ],

(依此类推......)

如何让jq从拥有它的所有对象中排除_children属性?那些以_开头的所有属性呢?

以下任何一项似乎都不起作用:

jq 'map(del (._children))'
jq 'map(if has("_children") then del (._children) end)'
jq 'del(._children)'
jq 'del(.[]._children)'
jq 'del(.[]|._children)'

我一直收到类似的错误:

jq: error (at <stdin>:1): Cannot index string with string "_children"

1 个答案:

答案 0 :(得分:1)

  

从包含它的所有对象中排除_children属性

如果您的jq有walk/1,那么您可以:

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

如果没有,首先包括其jq定义(易于谷歌),例如在〜/ .jq

  

以_?

开头的所有属性怎么样?

为此,您还可以使用walk/1。为了清晰和可维护性,定义辅助函数是有意义的:

def deleteall(f): with_entries(select(.key | f | not ));

您要调用的内容为:deleteall(startswith(&#34; _&#34;))