Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python JSON dump / append to .txt with each variable on new line

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.

like image 821
Victor S Avatar asked Jun 11 '13 22:06

Victor S


People also ask

How do you append data to an existing JSON file in Python?

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.

How do I add a new line to a JSON 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.

Does JSON dump append or overwrite?

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...}{

What is the difference between JSON dump and JSON dumps?

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.

What is the use of JSON dump in Python?

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.

Can JSON dump in Python write newline character and carriage?

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.

How do I write a JSON file in Python?

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.

What is the syntax for dump() method in Python?

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 .


2 Answers

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.

like image 167
agf Avatar answered Oct 19 '22 23:10

agf


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.

like image 28
Sayali Sonawane Avatar answered Oct 19 '22 23:10

Sayali Sonawane