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.

Answer from agf on Stack Overflow
🌐
JSON Lint
jsonlint.com › learn › read and write json files in python: load, loads, dump, and dumps
Read and Write JSON Files in Python: load, loads, dump, and dumps | JSONLint
August 14, 2026 - Python's built-in json module reads JSON files and strings without an additional package. Use json.load() for a file, json.loads() for a string, json.dump() to write a file, and json.dumps() to create a string.
🌐
Medium
galea.medium.com › how-to-love-jsonl-using-json-line-format-in-your-workflow-b6884f65175b
How to Love jsonl — using JSON Lines in your Workflow | by Alexander Galea | Medium
April 9, 2024 - I use jsonl for dumping raw “source of truth” data. From there it can be loaded in and processed by any part of the application and, if needed, dumped into a relational format (e.g. Postgres, MySQL, CSV). Here are python functions that can be used to write and read jsonl:
Discussions

Python JSON dump / append to .txt with each variable on new line - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
How can I write data to a json file and also write the variable name?
The resulting file you are creating here isn't a valid json document. If you printed a "\n" between each dump, it would be valid jsonl (json lines). I believe just storing one data structure instead of 3 seperate ones solves both of your issues: import json my_data = { "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(my_data, f) More on reddit.com
🌐 r/learnpython
8
4
August 13, 2022
python - Write multiple JSON lines to JSON file - Stack Overflow
I have a code that needs to read a JSON file with multiple lines, i.e: Copy{"c1-line1": "value", "c2-line1": "value"} {"c1-line2": "value", "c2-line2": "value"}... and, after change the keys values (already working), I need to write a new json file with these multiple lines, i.e: More on stackoverflow.com
🌐 stackoverflow.com
March 25, 2022
Python json.dumps() outputs all my data into one line but I want to have a new line for each entry - Stack Overflow
I am working with Python and some json data. I am looping through my data (which are all dictionaries) and when I print the loop values to my console, I get 1 dictionary per line. However, when I d... More on stackoverflow.com
🌐 stackoverflow.com
🌐
OneUptime
oneuptime.com › home › blog › how to read and write json files in python
How to Read and Write JSON Files in Python
January 25, 2026 - Notice that JSON's true becomes Python's True, and null becomes None. To write Python data to a JSON file, use json.dump():
🌐
Python
docs.python.org › 3 › library › json.html
JSON encoder and decoder — Python 3.14.7 documentation
Return a JSON string representation of a Python data structure, o. For example: >>> json.JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}' ... Encode the given object, o, and yield each string representation as available.
🌐
iO Flood
ioflood.com › blog › python-write-json-to-file
Python Guide: How to Write JSON to a File
February 1, 2024 - The jsonlines library is a simple Python library for working with JSONLines data. It’s particularly useful when dealing with large JSON files that need to be read or written line by line. Here’s an example of how to use jsonlines to write JSON data to a file:
🌐
GeeksforGeeks
geeksforgeeks.org › python › reading-and-writing-json-to-a-file-in-python
Reading and Writing JSON to a File in Python - GeeksforGeeks
It takes two parameters: dictionary: ... indentation · After converting the dictionary to a JSON object, simply write it to a file using the "write" function....
Published: August 5, 2025
Find elsewhere
🌐
Ml-gis-service
ml-gis-service.com › index.php › 2022 › 04 › 27 › toolbox-python-list-of-dicts-to-jsonl-json-lines
Toolbox: Python List of Dicts to JSONL (json lines) – Sp.4ML
April 27, 2022 - It is natural to think about this structure in terms of a Python list of dict(s). It could be a stream of website events with additional properties, for example, user actions or viewed products: {"user": "xyz", "action": "click", "element": "submit button"} {"user": "zla", "action": "products view", "items": ["product a", "product x"]} {"user": "iks", "action": "add to cart", "items": ["product b"], "item properties": {"price": 3.5, "color": "silver"}} ... import gzip import json def dicts_to_jsonl(data_list: list, filename: str, compress: bool = True) -> None: """ Method saves list of dicts into jsonl file.
🌐
Stack Abuse
stackabuse.com › reading-and-writing-json-to-a-file-in-python
Reading and Writing JSON to a File in Python
April 18, 2023 - If you encounter this edge-case, which has since been fixed in subsequent Python versions - try using json.dumps() instead, and write the string contents into a file instead of streaming the contents directly into a file.
🌐
Medium
medium.com › @caseyahenson › quick-tip-writing-newline-character-to-json-file-in-python-3705a2cd14fb
Quick Tip: Writing newline character to JSON file in Python | by Casey Henson | Medium
January 8, 2022 - A client may parse this JSON file and read the newline characters as returns, thereby creating a new paragraph after “Hello!”. Now let’s say you’re using Python (though this technique could apply to many languages) to generate this file and so you create your string and write the string to the file. You realize that Python has interpreted the newline characters as such and has applied line returns to the file itself.
🌐
Readthedocs
jsonlines.readthedocs.io
jsonlines — jsonlines documentation
jsonlines is a Python library to simplify working with jsonlines and ndjson data. This data format is straight-forward: it is simply one valid JSON value per line, encoded using UTF-8.
🌐
Stack Overflow
stackoverflow.com › questions › 38844203 › ideal-way-to-write-json-objects-in-new-lines-to-a-text-file-using-python
Ideal way to write JSON objects in new lines to a text file using Python - Stack Overflow
August 9, 2016 - I need to writing in this format only as without this newline everything goes into a single line in the output file and it't not very useful that way. ... It's not a very good method. The problem is that it will fail as soon as you have a json object that contains a newline character itself. ... @wvdz: which would be escaped if inside a string. json.dump() does not, by default, produce JSON documents with newlines.
🌐
Programiz
programiz.com › python-programming › json
Python JSON: Read, Write, Parse JSON (With Examples)
Here's a table showing Python objects and their equivalent conversion to JSON. To write JSON to a file in Python, we can use json.dump() method.