将 JSON 文件拆分为单独的文件 [英] Split a JSON file into separate files

查看:36
本文介绍了将 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.

推荐答案

这应该给你一个开始:

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屋!

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