My code creates a dictionary, which is then stored in a variable. I want to write each dictionary to a JSON file, but I want each dictionary to be on a new line.
My dictionary:
hostDict = {"key1": "val1", "key2": "val2", "key3": {"sub_key1": "sub_val2", "sub_key2": "sub_val2", "sub_key3": "sub_val3"}, "key4": "val4"}
Part of my code:
g = open('data.txt', 'a')
with g as outfile:
json.dump(hostDict, outfile)
This appends each dictionary to 'data.txt' but it does so inline. I want each dictionary entry to be on new line. Any advice would be appreciated.
Steps for Appending to a JSON File In Python, appending JSON to a file consists of the following steps: Read the JSON in Python dict or list object. Append the JSON to dict (or list ) object by modifying it. Write the updated dict (or list ) object into the original file.
In JSON object make sure that you are having a sentence where you need to print in different lines. Now in-order to print the statements in different lines we need to use '\\n' (backward slash). As we now know the technique to print in newlines, now just add '\\n' wherever you want.
it would append the new data. the problem with this approach is that it literally appends the data so that it ends up like {... original data...}{
json. dump() method used to write Python serialized object as JSON formatted data into a file. json. dumps() method is used to encodes any Python object into JSON formatted String.
The json.dump () method (without “ s ” in “dump”) used to write Python serialized object as JSON formatted data into a file. The json.dumps () method encodes any Python object into JSON formatted String.
JSON dump in Python writing newline character and carriage returns in file. - Stack Overflow JSON dump in Python writing newline character and carriage returns in file. Bookmark this question. Show activity on this post.
The json.dump () method (without “ s ” in “dump”) used to write Python serialized object as JSON formatted data into a file. The file type can be anything including text, JSON or even binary file. The json.dumps () method encodes any Python object into JSON formatted String.
The syntax for dump () method is, Serialize obj as a PYTHON JSON formatted stream to fp (a write () supporting file like object) using the conversion. You can specify the PYTHON JSON formatted data object in your program and use the dumps () method to write a data object to a Python string .
Your question is a little unclear. If you're generating hostDict
in a loop:
with open('data.txt', 'a') as outfile:
for hostDict in ....:
json.dump(hostDict, outfile)
outfile.write('\n')
If you mean you want each variable within hostDict
to be on a new line:
with open('data.txt', 'a') as outfile:
json.dump(hostDict, outfile, indent=2)
When the indent
keyword argument is set it automatically adds newlines.
To avoid confusion, paraphrasing both question and answer. I am assuming that user who posted this question wanted to save dictionary type object in JSON file format but when the user used json.dump
, this method dumped all its content in one line. Instead, he wanted to record each dictionary entry on a new line. To achieve this use:
with g as outfile:
json.dump(hostDict, outfile,indent=2)
Using indent = 2
helped me to dump each dictionary entry on a new line. Thank you @agf. Rewriting this answer to avoid confusion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With