preserving linebreaks in values in JSON key-value pairs preserving linebreaks in values in JSON key-value pairs json json

preserving linebreaks in values in JSON key-value pairs


If you are displaying (or inserting) the json value directly in HTML, you can't use it as it is because in html new lines are ignored and replaced by a space.

For example if you have:

<p>Hello,I'm in other line.</p>

It will be represented as:

Hello, I'm in other line.

You must convert the new lines to paragraph or <br>, for example:

<p>Hello,<br>I'm in other line.</p>

That will be show as:

Hello,

I'm in other line

If this is your case, you can simply use String.replace to change \n into <br>\n.


If you're displaying it in HTML,
will simple do.Or in a text area or something, try "\r\n", wrapped in double quotes.Double backslashes are to escape.