32

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.

3
  • Possible duplicate of Multiline strings in JSON Commented Feb 11, 2018 at 20:02
  • 4
    @Francisco Puga: only to 70% Commented Feb 11, 2018 at 20:07
  • 1
    This is not a duplicate because that one is about \n inside string while this one is about how the same content is represented differently (more readable) in the JSON source. Commented Aug 29, 2018 at 8:13

4 Answers 4

13

You can use multiple lines string representation in JavaScript:

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

Tried in console. It works.

Sign up to request clarification or add additional context in comments.

Comments

6

Use an array:

{
    "id": 550,
    "text": ["this is long text ",
            "very-very-very long text ",
            "longer than you can imagine."]
}

Assuming that this goes in this.messages, use in code

this.messages.text.join();

1 Comment

Thanks for the hint to use this approach. I had to use this.messages.text.join(""); specifying the join-deliminator. Without it (empty brackts), my string was deliminated by commas
0

According to JSONLint.com, the following multiline JSON is valid. So in short, yes, you can hit enter and break it into different lines.

{
    "a": 10,
    "b": 12,
    "c": 19
}

EDIT: I think I misread your question. I don't think you're able to break between a string like below. That does not work.

{
    "a": "abcde
    fg",
    "b": 12,
    "c": 19
}

2 Comments

Can you explain the context? What language are you writing in, why do you need a line break, etc.?
You may use an array of strings as in "a" : ["abcde", "fg"], Then in your js code use either join() (for a long line of text as "abcdefg") or join("\n") for a text representation of two lines.
-5

You can't do it, you need to write the value in one line, if you want write in the next row, you need to put \n in your code

2 Comments

The question says clearly that it should be rendered as one row
He explicitly said that he is “not asking about rendering strings as multiline.” Perhaps you could give an example of how to work around this problem using JavaScript? Sorta like Pinal’s answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.