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...
Just include paragraph tags to make it look neat and more efficient.
Example: "body": "
Paragraph 1 here.
Paragraph 2 here.
",Use regex to replace all occurrences of
\r\n
with\\n
.This worked for me in scala.
for multiline i'm using:
write any text in notepad++ with multi multi line then press ctl+a and ctl+j to arrange them in one line
In json file write :
Is it possible to have multi-line strings in JSON?
Yes. I just tested this now with my Firefox web browser by pressing F12, clicking console and typing at the bottom of the screen.
Object x has just been created from a JSON format string containing a multi-line string.
x.text is displayed showing that it is a multi-line string.
These two tests show that Firefox's Javascript interpreter is happy to create and use JSON with multiline strings.
More tests with
JSON.stringify
andJSON.parse
showed the Javascript interpreter can convert an object containing multiline strings to JSON and parse it back again with no problem at all.I have in the past stored the complete works of Shakespeare as a property in a JSON object and then sent it over the internet, uncorrupted.
Example
Here is a two line string entered over three lines
We can display the object
giving
or the string
giving
The end of lines in the string result from using \n and the multiple input lines are achieved using just \ at the end of the line.
In practice you might want to synchronize your line endings with the ones in the string, e.g.
Multi-Line String Length
Note that the string with the newline is not longer than the string with the space. Even though two characters were typed on the keyboard ('\' and 'n'), only one character is stored in the string.
Try using
base64
encoded string value. Worked for me most of the time.after
base64
encoded string look likebase64 encoding and decoding is available in all languages.