How to code long JSON value strings as multiline?

Paul picture Paul · Jan 12, 2014 · Viewed 31.7k times · Source

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.

Answer

Pinal picture Pinal · Jan 12, 2014

You can use multiple lines string representation in javascript:

JSON.parse('{"a" : "a\
asd"}')

Tried in console. It works.