使用jq连接多个文件中的JSON数组 [英] Use jq to concatenate JSON arrays in multiple files
本文介绍了使用jq连接多个文件中的JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一系列包含一系列记录的JSON文件,例如
I have a series of JSON files containing an array of records, e.g.
$ cat f1.json
{
"records": [
{"a": 1},
{"a": 3}
]
}
$ cat f2.json
{
"records": [
{"a": 2}
]
}
我想1)从每个记录中提取一个字段,并2)输出包含所有输入文件中所有字段值的单个数组.
I want to 1) extract a single field from each record and 2) output a single array containing all the field values from all input files.
第一部分很简单:
jq '.records | map(.a)' f?.json
[
1,
3
]
[
2
]
但是我不知道如何获取jq
来将这些输出数组连接成一个数组!
But I cannot figure out how to get jq
to concatenate those output arrays into a single array!
我没有嫁给jq
;如有必要,我会很乐意使用其他工具.但是我很想知道如何使用jq
来做到这一点,因为这是我一直试图找出的年 .
I'm not married to jq
; I'll happily use another tool if necessary. But I would love to know how to do this with jq
, because it's something I have been trying to figure out for years.
推荐答案
使用-s
(或--slurp
):
jq -s 'map(.records[].a)' f?.json
这篇关于使用jq连接多个文件中的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文