1

I'm writing a python script to run through a csv file and convert it into one .json file. That would mean the .json file would have multiple json objects inside it. I've got most of the script working but the part I am confused with (and I've looked online for a while now) is how multiple JSON objects should be stored in one .json file. Is it either of the ways below? :

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

or

{"index": {"_index": "jobs", "_id": 119556, "_type": "2014_jobs"}}
{"index2": {"_index": "jobs", "_id": 119700, "_type": "2014_jobs"}}
{"index3": {"_index": "jobs", "_id": 118701, "_type": "2014_jobs"}}
4
  • For more context, I'm writing to script to get data to run elasticsearch on it but the data isn't being accepted and the error says its due to the JSON formatting Commented May 27, 2016 at 7:21
  • 1
    put all json objects in a list Commented May 27, 2016 at 7:25
  • so after generating the complete list add it like json.dump(list, jsonfile)? Commented May 27, 2016 at 7:39
  • Hey hasson, thanks for your help! I managed to get the script to generate the objects the way I wanted :) Commented May 27, 2016 at 8:17

1 Answer 1

1

Looks pritty close to the solution. May you should use a list:

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

This would be my solution. Hope it helped

Sign up to request clarification or add additional context in comments.

1 Comment

I realized I needed to submit multiple documents anyway but thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.