I'm writing some data files in JSON format and would like to have some really long string values split over multiple lines. Using python's JSON module I get a whole lot of errors, whether I use \
or \n
as an escape.
Is it possible to have multi-line strings in JSON? It's mostly for visual comfort so I suppose I can just turn word wrap on in my editor, but I'm just kinda curious...
you could simply run the code below with your desire multiline string between tilde
~
character:JSON does not allow real line-breaks. You need to replace all the line breaks with
\n
.eg:
"first line second line"
can saved with:
"first line\nsecond line"
Note:
for
Python
, should written as:"first line\\nsecond line"
which
\\
is for escape backslash, otherwise python will treat\n
as control character:new line
I have had to do this for a small Node.js project and found this work-around:
This looks quite neat to me, appart from that I have to use double quotes everywhere. Though otherwise, I could, perhaps, use YAML, but that has other pitfalls and is not supported natively. Once parsed, I just use
myData.modify_head.join('\n')
ormyData.modify_head.join()
, depending upon whether I want a line break after each string or not.You can try place
<div></div>
between sentences.put the multiline text on txt file and then
(it work's on mongoDB)