0

I am trying to write JSON data into the file which is writing in one line as below:

{"AbandonmentDate": "", "Abstract": "", "Name": "ABC"}{"AbandonmentDate": "", "Abstract": "", "Name": "ABC"}

My code is as follow:

with open(file_name, 'w') as file:
            for data in results:
                saveData = {}
                for k,v in data.items():
                    if v:
                        saveData[k] = v
                    else:
                        saveData[k] = ''
                print (json.dumps(saveData))
                file.write(json.dumps(saveData, ensure_ascii=False))
        file.close()

What I need it as below format:

{"AbandonmentDate": "", "Abstract": "", "Name": "ABC"}
{"AbandonmentDate": "", "Abstract": "", "Name": "ABC"}

I tried several ways from the various answer from StackOverflow, however, I am unable to get it? Is there any way to do it?

10
  • 1
    Use json.dump()? Commented Apr 9, 2019 at 11:05
  • What is the difference between your first and last example outputs? Commented Apr 9, 2019 at 11:06
  • 4
    So you're asking how to add a file.write('\n') to your loop? Commented Apr 9, 2019 at 11:07
  • 1
    Missing , in your json data? Commented Apr 9, 2019 at 11:09
  • 2
    Just a side note: you don't need to close the file if you are using with Commented Apr 9, 2019 at 11:11

2 Answers 2

4

Using json_dump:

j_data = {"AbandonmentDate": "", "Abstract": "", "Name": "ABC"},{"AbandonmentDate": "", "Abstract": "", "Name": "ABC"}

import json
with open('j_data_file.json', 'w') as outfile:
    json.dump(j_data, outfile,indent=4)

OUTPUT:

[
    {
        "AbandonmentDate": "",
        "Abstract": "",
        "Name": "ABC"
    },
    {
        "AbandonmentDate": "",
        "Abstract": "",
        "Name": "ABC"
    }
]

EDIT:

If you really want to have the elements printed on new lines, iterate over the data:

j_data = {"AbandonmentDate": "", "Abstract": "", "Name": "ABC"},{"AbandonmentDate": "", "Abstract": "", "Name": "ABC"}

import json
with open('j_data_file.json', 'w') as outfile:
    for elem in j_data:
        json.dump(elem, outfile)
        outfile.write('\n')

OUTPUT:

{"AbandonmentDate": "", "Abstract": "", "Name": "ABC"}
{"AbandonmentDate": "", "Abstract": "", "Name": "ABC"}
Sign up to request clarification or add additional context in comments.

10 Comments

This is nothing like the output that the OP seems to want.
@quamrana Perhaps, the way it should be no? Adding an alternative with a newline separator as well.
this is pretty json, somehow in my further operations, it is not supported.
@DirtyBit Certainly this is the code I would write. I don't know why the OP wants the format they specify.
@quamrana Indeed, added the way OP wanted it as well! :)
|
3

Assuming your json is like:

yourjson = [
    {"AbandonmentDate": "", "Abstract": "", "Name": "ABC"},
    {"AbandonmentDate": "", "Abstract": "", "Name": "ABC"}
]

then you need only to do this:

with open("outfile.txt", "w") as pf:
    for obj in yourjson:
        pf.write(json.dumps(obj) + "\n")

Comments

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.