jq,拆分数组的巨大json并保存到以值命名的文件中 [英] jq, split a huge json of array and save into file named with a value

查看:118
本文介绍了jq,拆分数组的巨大json并保存到以值命名的文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含对象数组的json,每个对象在以下位置都包含一个唯一值:

i have a json containing an array of objects, every object contains a unique value in:

"id":"value"

我遵循了这个其他答案,我可以使用jq和awk将整个文档分成多个文件

i've followed this other answer and i can split the whole document in multiple files using jq and awk

jq -c ".[]" big.json | gawk '{print > "doc00" NR ".json";}'

这样,输出文件将按顺序命名.
我如何使用id值命名文件?

in this way the output files are named sequentially.
how i can name the files using the id value?

推荐答案

对于数组中的每个元素,请在两行中分别打印id和元素本身,因此您可以从奇数行中获取id并打印偶数行到以id命名的文件.

For each element in the array, print id and the element itself in two separate lines, thus you can grab the id from odd numbered lines and print even numbered lines to files named with id.

jq -cr '.[] | .id, .' big.json | awk 'NR%2{f=$0".json";next} {print >f;close(f)}'

这篇关于jq,拆分数组的巨大json并保存到以值命名的文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆