json 文档拆分工具,如何在每个文档中将json拆分为多个文件

I have a large json file that contains thousands of documents:

[

{

"_id": "document1",

"fields": [ ... ]

},

{

"_id": "document2",

"fields": [ ... ]

},

...

]

I'd like to split this json file so that each json file contains a single document, and name them accordingly:

document1.json, document2.json, ...

For example, document1.json will contain:

{

"_id": "document1",

"fields": [ ... ]

}

I have no knowledge of jq API, and I'm struggling to find an answer (I've find a similar question, but slightly different :( )

解决方案

Here is a Python solution to your problem.

Don't forget to change the in_file_path to the location of your big JSON file.

import json

in_file_path='path/to/file.json' # Change me!

with open(in_file_path,'r') as in_json_file:

# Read the file and convert it to a dictionary

json_obj_list = json.load(in_json_file)

for json_obj in json_obj_list:

filename=json_obj['_id']+'.json'

with open(filename, 'w') as out_json_file:

# Save each obj to their respective filepath

# with pretty formatting thanks to `indent=4`

json.dump(json_obj, out_json_file, indent=4)

Side Note: I ran this in Python3, it should work in Python2 as well

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值