Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to code long JSON value strings as multiline?

Tags:

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.

like image 514
Paul Avatar asked Jan 12 '14 16:01

Paul


People also ask

Can a JSON value contain a multiline string?

If you want to store multiline string in a file then your file will not store the valid json object.

How do you break a long string into multiple lines?

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.

How do you insert a line break in JSON?

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.


1 Answers

You can use multiple lines string representation in JavaScript:

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

Tried in console. It works.

like image 83
Pinal Avatar answered Sep 16 '22 14:09

Pinal