Your input appears to be a sequence of Python objects; it certainly is not valid a JSON document.
If you have a list of Python dictionaries, then all you have to do is dump each entry into a file separately, followed by a newline:
import json
with open('output.jsonl', 'w') as outfile:
for entry in JSON_file:
json.dump(entry, outfile)
outfile.write('\n')
The default configuration for the json module is to output JSON without newlines embedded.
Assuming your A, B and C names are really strings, that would produce:
{"index": 1, "met": "1043205", "no": "A"}
{"index": 2, "met": "000031043206", "no": "B"}
{"index": 3, "met": "0031043207", "no": "C"}
If you started with a JSON document containing a list of entries, just parse that document first with json.load()/json.loads().
Your input appears to be a sequence of Python objects; it certainly is not valid a JSON document.
If you have a list of Python dictionaries, then all you have to do is dump each entry into a file separately, followed by a newline:
import json
with open('output.jsonl', 'w') as outfile:
for entry in JSON_file:
json.dump(entry, outfile)
outfile.write('\n')
The default configuration for the json module is to output JSON without newlines embedded.
Assuming your A, B and C names are really strings, that would produce:
{"index": 1, "met": "1043205", "no": "A"}
{"index": 2, "met": "000031043206", "no": "B"}
{"index": 3, "met": "0031043207", "no": "C"}
If you started with a JSON document containing a list of entries, just parse that document first with json.load()/json.loads().
The jsonlines package is made exactly for your use case:
import jsonlines
items = [
{'a': 1, 'b': 2},
{'a', 123, 'b': 456},
]
with jsonlines.open('output.jsonl', 'w') as writer:
writer.write_all(items)
(Yes, I wrote it years after you posted your original question.)
Python JSON dump / append to .txt with each variable on new line - Stack Overflow
How can I add \n(newline) In Json string
jsonl format - usage - Prodigy Support
Python write valid json with newlines to file - Stack Overflow
ยป pip install json-lines
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.
You ran into the pitfall of neglecting the fact that the \ character is also an escape sequence character in Python. Try printing out the last example instead of calling json.loads:
>>> print('{"mystring": "Line 1\nLine 2"}')
{"mystring": "Line 1
Line 2"}
No way the above is valid JSON. What if the \ character is correctly encoded?
>>> print('{"mystring": "Line 1\\nLine 2"}')
{"mystring": "Line 1\nLine 2"}
Much better, you can then:
>>> json.loads('{"mystring": "Line 1\\nLine 2"}')
{'mystring': 'Line 1\nLine 2'}
Alternatively, if you really appreciate being able to copy some text from some other buffer and paste it into your live interpreter to do decode, you may consider using the raw modifier for your string:
>>> print(r'{"mystring": "Line 1\nLine 2"}')
{"mystring": "Line 1\nLine 2"}
>>> json.loads(r'{"mystring": "Line 1\nLine 2"}')
{'mystring': 'Line 1\nLine 2'}
See that the \ is no longer automatically escaping with the newline.
Also see: How do I handle newlines in JSON? and note how this is not a problem that exists strictly within Python.
The reason for this:
print(json_data)
# -> {"mystring": "Line 1\nLine 2"}
Is that \\ is a valid escape sequence that ends up as a single backslash \ when trying to print it.
The data in the json file is valid, as the parser is able to parse it :)
The confusion stems from the fact that when you try to print a string with escape sequences those get interpreted. And the sequence \\n is interpreted as \n
Let's say I want to write two lists and a dict to a JSON file. I can do so with the following code:
import json
x = [1, 2, 3]
y = [4, 5, 6]
z = {"1": 1, "2": 2}
with open("test.json", "a", encoding="utf-8") as f:
json.dump(x, f)
json.dump(y, f)
json.dump(z, f)But what if I want the variable names written to the file as well? My goal is to be able to read / write / append to the two lists and the dict at a later time. To do so, I would need to access them by variable name. How can I achieve this?
If you want to produce valid JSON file you need to write all values at once, not one value at a time (which will produce ndjson or JSON lines)
So, for a valid JSON
values = [{"first_name": "John", "last_name": "Smith", "food": "corn"},
{"first_name": "Jane", "last_name": "Doe", "food": "soup"}]
import json
with open('some_file.json', 'w') as f:
json.dump(values, f, indent=4)
some_file.json
[
{
"first_name": "John",
"last_name": "Smith",
"food": "corn"
},
{
"first_name": "Jane",
"last_name": "Doe",
"food": "soup"
}
]
if you really need ndjson - you can use ndjson package (need install via pip from PyPi).
import ndjson
with open('some_file2.ndjson', 'w') as f:
ndjson.dump(values, f)
some_file2.ndjson
{"first_name": "John", "last_name": "Smith", "food": "corn"}
{"first_name": "Jane", "last_name": "Doe", "food": "soup"}
Alternative to ndjson package is jsonlines package
You can newline after each
f.write(json.dumps(value, sort_keys=True, indent=0))
like this - f.write('\n')