IMPORTANT: I am not asking about rendering strings as multiline.
I am talking about splitting a long string in a JSON into multiple lines in my source code when this string should logically be on a single line.
In short: I want source line breaking rules similar to HTML.
{ "id": 550, "text": "this is long text " "very-very-very long text " "longer than you can imagine." }
This text should be rendered as:
this is long text very-very-very long text longer than you can imagine.
The JSON is being referenced in JavaScript.
This is not a duplicate of Multiline strings in JSON because this question strongly refers to JavaScript and that question has no clear accepted answer.
If you want to store multiline string in a file then your file will not store the valid json object.
Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.
In JSON object make sure that you are having a sentence where you need to print in different lines. Now in-order to print the statements in different lines we need to use '\\n' (backward slash). As we now know the technique to print in newlines, now just add '\\n' wherever you want.
You can use multiple lines string representation in JavaScript:
JSON.parse('{"a" : "a\ asd"}')
Tried in console. It works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With