Formatting json strings in Python
<p>I have a query that runs and creates a dataframe like below:</p>

<pre><code>Group   Hour
G1        0
G1        4
G2        1
G3        5
</code></pre>

<p>I then write the dataframe to a json file using</p>

<pre><code>out = df1.to_json(orient='records')[1:-1].replace('},{', '} {')
</code></pre>

<p>Currently my output looks like</p>

<pre><code>{"Group":"G1","Hour":0} {"Group":"G2","Hour":4} {"Group":"G2","Hour":1}
</code></pre>

<p>It is a continuous stream of lines. I am trying to make each json document to be in a separate line like below:</p>

<pre><code>{"Group":"G1","Hour":0} 
{"Group":"G2","Hour":4} 
{"Group":"G2","Hour":1}
</code></pre>

<p>Not sure whether there is a way to achieve this in Python. Any help would be appreciated.</p>

<p>Just change the <code>.replace('},{', '} {')</code> part to <code>.replace('},{', '}\n{')</code>?</p>

python json pandas