简体   繁体   English

使用 jq 连接 JSON 文件的目录

[英]Using jq to concatenate directory of JSON files

I have a directory of about 100 JSON files, each an array of 100 simple records, that I want to concatenate into one file for inclusion as static data in an app, so I don't have to make repeated API calls to retrieve small pieces. I have a directory of about 100 JSON files, each an array of 100 simple records, that I want to concatenate into one file for inclusion as static data in an app, so I don't have to make repeated API calls to retrieve small pieces . (I'm limited to downloading only 100 records at a time; that's why I have 100 short files.) (我一次只能下载 100 条记录;这就是我有 100 个短文件的原因。)

Here's a sample file, shortened to two records for display here:这是一个示例文件,缩短为两条记录以便在此处显示:

[
 {
   "id": 11531,
   "title": "category 1",
   "count": 5
 },
 {
   "id": 11532,
   "title": "category 2",
   "count": 5
 }
]

My research led to a solution that works but only for two files with two records each:我的研究导致了一个有效的解决方案,但适用于两个文件,每个文件有两条记录:

jq -s '.[0] + .[1]' file1.json file2.json > output.json

This source also suggested this line would work to handle a directory (right now only two files in it):该消息来源还建议此行可以处理目录(现在只有两个文件):

jq -s 'reduce .[] as $item ({}; . * $item)' json_files/* > output.json

but I get an error:但我收到一个错误:

jq: error (at json_files/categories-11-20.json:0): object ({}) and array ([{"id":1153...) cannot be multiplied

I thought maybe the problem was the * trying to multiply, so I tried + in that place, but I get a ... cannot be added.我想也许问题是*试图乘法,所以我在那个地方尝试了+ ,但我得到了一个... cannot be added. message.信息。

Is there a way to do this through jq or is there a better tool?有没有办法通过 jq 做到这一点,或者有更好的工具吗?

The simplest and perfectly reasonable approach would be to use the -s command-line option and add along the following lines:最简单且完全合理的方法是使用 -s 命令行选项并add以下行:

jq -s add json_files/* 

Of course you may wish to specify the list of files differently.当然,您可能希望以不同的方式指定文件列表。 The order in which they are specified is also significant.指定它们的顺序也很重要。

Notes:笔记:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM