4

在大型 json 文件中,我想从嵌套列表中删除一些元素,但保留文档的整体结构。

我的示例输入它(但真正的大到足以要求流式传输)。

{
  "keep_untouched": {
    "keep_this": [
      "this",
      "list"
    ]
  },
  "filter_this":
  [
    {"keep" : "true"},
    {
      "keep": "true",
      "extra": "keeper"
    } ,
    {
      "keep": "false",
      "extra": "non-keeper"
    }
  ]
}

所需的输出只删除了“filter_this”块的一个元素:

{
  "keep_untouched": {
    "keep_this": [
      "this",
      "list"
    ]
  },
  "filter_this":
  [
    {"keep" : "true"},
    {
      "keep": "true",
      "extra": "keeper"
    } ,
  ]
}

处理这种情况的标准方法似乎是使用 'truncate_stream' 来重构流对象,然后以通常的 jq 方式过滤这些对象。具体来说,命令:

jq -nc --stream 'fromstream(1|truncate_stream(inputs))' 

提供对对象流的访问:

{"keep_this":["this","list"]}
[{"keep":"true"},{"keep":"true","extra":"keeper"}, 
 {"keep":"false","extra":"non-keeper"}]

此时很容易过滤所需的对象。但是,这会从其父对象的上下文中剥离结果,这不是我想要的。

查看流式结构:

[["keep_untouched","keep_this",0],"this"]
[["keep_untouched","keep_this",1],"list"]
[["keep_untouched","keep_this",1]]
[["keep_untouched","keep_this"]]
[["filter_this",0,"keep"],"true"]
[["filter_this",0,"keep"]]
[["filter_this",1,"keep"],"true"]
[["filter_this",1,"extra"],"keeper"]
[["filter_this",1,"extra"]]
[["filter_this",2,"keep"],"false"]
[["filter_this",2,"extra"],"non-keeper"]
[["filter_this",2,"extra"]]
[["filter_this",2]]
[["filter_this"]]

看来我需要选择所有“filter_this”行,仅截断这些行(使用“truncate_stream”),将这些行重建为对象(使用“from_stream”),过滤它们,然后将对象转换回流数据格式(使用“tostream”)加入“保持不变”行的流,这些行仍然是流格式。那时,可以重新构建整个 json。如果这是正确的方法——这对我来说似乎过于复杂——我该怎么做?或者,还有更好的方法?

4

3 回答 3

2

如果您的输入文件包含一个非常大的 JSON 实体,对于常规 jq 解析器在您的环境中无法处理,那么您很可能没有足够的内存来重构 JSON 文档。

有了这个警告,以下可能值得一试。关键的见解是可以使用reduce.

为了清楚起见,以下使用了一堆临时文件:

TMP=/tmp/$$

jq -c --stream 'select(length==2)' input.json > $TMP.streamed

jq -c 'select(.[0][0] != "filter_this")' $TMP.streamed > $TMP.1

jq -c 'select(.[0][0] == "filter_this")' $TMP.streamed |
  jq -nc 'reduce inputs as [$p,$x] (null; setpath($p;$x))
          | .filter_this |= map(select(.keep=="true"))
          | tostream
          | select(length==2)' > $TMP.2

# Reconstruction
jq -n 'reduce inputs as [$p,$x] (null; setpath($p;$x))' $TMP.1 $TMP.2

输出

{
  "keep_untouched": {
    "keep_this": [
      "this",
      "list"
    ]
  },
  "filter_this": [
    {
      "keep": "true"
    },
    {
      "keep": "true",
      "extra": "keeper"
    }
  ]
}
于 2018-03-28T17:30:58.183 回答
1

非常感谢@peak。我发现他的方法非常有用,但在性能方面不切实际。不过,窃取了@peak 的一些想法,我想出了以下几点:

提取“父”对象:

jq -c --stream 'select(length==2)' input.json | 
  jq -c 'select(.[0][0] != "filter_this")'  | 
  jq -n 'reduce inputs as [$p,$x] (null; setpath($p;$x))' > $TMP.parent

提取 'keepers' - 虽然这意味着读取文件两次 (:-<):

jq -nc --stream '[fromstream(2|truncate_stream(inputs))
                  | select(type == "object" and .keep == "true")]             
                ' input.json > $TMP.keepers

将过滤后的列表插入到父对象中。

jq -nc -s 'inputs as $items
           | $items[0] as $parent
           | $parent
           | .filter_this |= $items[1]
          '  $TMP.parent $TMP.keepers > result.json
于 2018-03-29T16:32:11.410 回答
1

这是@PeteC 脚本的简化版本。它需要更少的 jq 调用。

在这两种情况下,请注意调用使用“2|truncate_stream(_)”的 jq 需要比 1.5 更新的 jq 版本。

TMP=/tmp/$$

INPUT=input.json

# Extract all but .filter_this
< $INPUT jq -c --stream 'select(length==2 and .[0][0] != "filter_this")' |
    jq -nc 'reduce inputs as [$p,$x] (null; setpath($p;$x))
           ' > $TMP.parent

# Need jq > 1.5
# Extract the 'keepers'
< $INPUT jq -n -c --stream '
  [fromstream(2|truncate_stream(inputs))
   | select(type == "object" and .keep == "true")]
  ' $INPUT > $TMP.keepers

# Insert the filtered list into the parent object:
jq -s '. as $in | .[0] | (.filter_this |= $in[1])
      ' $TMP.parent $TMP.keepers > result.json
于 2018-03-29T22:30:30.467 回答