How to write/print the lines breaks of a attribute of a json on Pyhon
<p>I have a little problema here.</p>

<p>I have the follow situation:</p>

<ul>
<li>I have the following json file</li>
</ul>

<blockquote>
  <p>{ <br/> 
  &nbsp;&nbsp;&nbsp;"name":"whatever",<br/>
  &nbsp;&nbsp;&nbsp;"lyric":"here comes the music <br/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;another line <br/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whatever... <br/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bla bla bla <br/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final"<br/>
  }</p>
</blockquote>

<ul>
<li><p>the next step would be loading the json file and change the attribute "name" to the "songs real name".</p></li>
<li><p>The final step would be writing the json to an output file, keepint the same input format (to make easier the json reading ), but it does print the lines break as scaped characters, like that:</p></li>
</ul>

<blockquote>
  <p>{ <br/> 
  &nbsp;&nbsp;&nbsp;"name":"songs real name",<br/>
  &nbsp;&nbsp;&nbsp;"lyric":"here comes the music  \n
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;another line \n
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whatever... \n
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bla bla bla \n
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final"<br/>
  }</p>
</blockquote>

<p>Desired output:</p>

<blockquote>
  <p>{ <br/> 
  &nbsp;&nbsp;&nbsp;"name":"songs real name",<br/>
  &nbsp;&nbsp;&nbsp;"lyric":"here comes the music <br/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;another line <br/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whatever... <br/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bla bla bla <br/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final"<br/>
  }</p>
</blockquote>

<p>That's the python code:</p>

<pre><code>with open(file, "r") as data_file:    
    json_data=data_file.read()
data_file.close()
json_data["name"]="song's real name"

with open(outputfile)
    jsonFile.write(json.dumps(data,indent=4, sort_keys=True)
</code></pre>

<p>everything works fine, except that it is not printing the lines break.</p>

<p>Thanks, I appreciate your help.</p>

<p>I have got the answer, basicaly what I did was a replace over a json.dumps:</p>

<pre><code>with open(file, "r") as data_file:    
json_data=data_file.read()
data_file.close()
json_data["name"]="song's real name"

with open(outputfile)
    jsonFile.write(json.dumps(data,indent=4, sort_keys=True, separators=(',', ': ')).replace('\\n','\n'))
</code></pre>

<p>after that, I got the output file as I would like:</p>

<blockquote>
  <p>{ <BR/>
  &nbsp;&nbsp;"name"&nbsp;:&nbsp;"songs real name", <BR/>
  &nbsp;&nbsp;"lyric"&nbsp;:&nbsp;"here comes the music <BR/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;another line <BR/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whatever... <BR/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bla bla bla <BR/>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final"<BR/>
  }<BR/></p>
</blockquote>

<p>Thank you guys.</p>

python json