【问题标题】:Splitting / chunking JSON files with JQ in Bash or Fish shell?在 Bash 或 Fish shell 中使用 JQ 拆分/分块 JSON 文件?
【发布时间】:2016-03-09 01:46:47
【问题描述】:

我一直在使用奇妙的JQ library 来解析和提取 JSON 数据以方便重新导入。我能够很容易地提取范围,但不确定如何在脚本中循环并检测文件的结尾,最好是在 bash 或 fish shell 脚本中。

给定一个包裹在 "results" 字典中的 JSON 文件,我如何检测文件的结尾?

通过测试,我可以看到我会得到一个嵌套在我想要的结构中的空数组,但是如何检测文件结束条件呢?:

jq '{ "results": .results[0:500] }' Foo.json > 0000-0500/Foo.json

谢谢!

【问题讨论】:

  • JSON 不处理文件,所以不清楚你在问什么。
  • 这既不是 bash 也不是 fish 问题,所以我删除了这些标签。
  • @chepner 关键是内容是 JSON,我需要拆分成原子 json 元素,最好是使用 bash 或 fish,以及标签。

标签: json partitioning jq filesplitting


【解决方案1】:

我建议使用 jq 将数组拆分为所需的 JSON 对象流(每行一个),然后使用其他工具(例如 awk)填充文件。以下是第一部分的完成方式:

def splitup(n):
  def _split:
    if length == 0 then empty
    else .[0:n], (.[n:] | _split)
    end;
  if n == 0 then empty elif n > 0 then _split else reverse|splitup(-n) end;

# For the sake of illustration:
def data: { results: [range(0,20)]};

data | .results | {results: splitup(5) }

调用:

$ jq -nc -f splitup.jq
{"results":[0,1,2,3,4]}
{"results":[5,6,7,8,9]}
{"results":[10,11,12,13,14]}
{"results":[15,16,17,18,19]}

对于第二部分,您可以(例如)将 jq 输出通过管道传输到:

  awk '{ file="file."++n; print > file; close(file); }'

您可能感兴趣的一个变体是让 jq 过滤器在交替行上同时发出文件名和 JSON; awk 脚本也会读取文件名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-24
    • 2019-02-28
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多