2017-05-25 57 views
1

我有以下数据集:你如何排除财产?

[ 
    { 
    "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" 
+0

请提供JSON的*完整*样品可以用作输入。 – chepner

+0

@chepner我拥有的东西足以满足我的用例。 – Qix

+0

@Qix - 如果没有看到更多的输入信息,就很难确定报告错误的原因。 – peak

回答

1

排除有它

所有对象_children财产

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

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

如果不是,首先包括其jq定义(易于googleable)例如in〜/ .jq

以_开头的所有属性怎么样?

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

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

,你会调用为:deleteall(startswith(“_”))

+0

整洁:)谢谢! – Qix