将多个JSON对象写入一个.json文件

时间:2016-05-27 07:20:45

标签: python json

我正在编写一个python脚本来运行csv文件并将其转换为一个.json文件。这意味着.json文件里面会有多个json对象。我已经完成了大部分脚本工作,但我感到困惑的部分(我现在在网上看了一段时间)是多个JSON对象应该如何存储在一个.json文件中。它是以下两种方式之一? :

{
    "index": {
        "_index": "jobs", 
        "_id": 119556, 
        "_type": "2014_jobs"
    },
    "index2": {
        "_index": "jobs", 
        "_id": 119700, 
        "_type": "2014_jobs"
    },
    "index3": {
        "_index": "jobs", 
        "_id": 118701, 
        "_type": "2014_jobs"
    },           
}

{"index": {"_index": "jobs", "_id": 119556, "_type": "2014_jobs"}}
{"index2": {"_index": "jobs", "_id": 119700, "_type": "2014_jobs"}}
{"index3": {"_index": "jobs", "_id": 118701, "_type": "2014_jobs"}}

1 个答案:

答案 0 :(得分:0)

看起来很接近解决方案。你可以使用一个列表:

{
 "data":[
    {
        "_index": "jobs", 
        "_id": 119556, 
        "_type": "2014_jobs"
    },
    {
        "_index": "jobs", 
        "_id": 119700, 
        "_type": "2014_jobs"
    },
    {
        "_index": "jobs", 
        "_id": 118701, 
        "_type": "2014_jobs"
    }]           
}

这将是我的解决方案。希望它有所帮助

相关问题