将JSON文件拆分为单独的文件 [英] Split a JSON file into separate files
本文介绍了将JSON文件拆分为单独的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大的JSON文件,它是对象的对象,我想在对象键之后将其拆分成单独的文件名.是否可以使用jq或任何其他现成的工具来实现这一目标?
I have a large JSON file that is an object of objects, which I would like to split into separate files name after object keys. Is it possible to achieve this using jq or any other off-the-shelf tools?
原始JSON格式如下
{ "item1": {...}, "item2": {...}, ...}
鉴于此输入,我想生成文件item1.json,item2.json等.
Given this input I would like to produce files item1.json, item2.json etc.
推荐答案
这应该为您提供一个起点:
This should give you a start:
for f in `cat input.json | jq -r 'keys[]'` ; do
cat input.json | jq ".$f" > $f.json
done
或者当您坚持使用一些更卑鄙的语法时,例如某些人似乎更喜欢:
or when you insist on more bashy syntax like some seem to prefer:
for f in $(jq -r 'keys[]') ; do
jq ".[\"$f\"]" < input.json > "$f.json"
done < input.json
这篇关于将JSON文件拆分为单独的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文