I am not sure if line breaks are allowed in JSON values. I certainly am unable to create the following in JSON
{"foo": "I am not sure if line breaks are
allowed in JSON values. I certainly
am unable to create the following in JSON"}
The following certainly does not work
{"foo": "I am not sure if line breaks are\nallowed in JSON values. I certainly\nam unable to create the following in JSON"}
Preamble: I want to send a long message like above either to the browser or to the console app and display it neatly formatted so it is legible to the user.
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.
Related
If you have a text file with many lines of text, is there a readily available way to convert it into the JSON Lines format?
Example text file contains:
This is the first line.
This is the "second" line.
This is the \third/ line.
This is the {fourth} line;
Example JSON Lines (.jsonl) file:
{"text": "This is the first line."}
{"text": "This is the \"second\" line."}
{"text": "This is the \\third\/ line."}
{"text": "This is the {fourth} line;"}
I was hoping there is a simple way to sort of linearly transform it like this, while escaping the special characters for JSON. Are there online or (CLI) tools for the Mac that can do this?
A browser is the most handy way to access an interpreter nowadays. Here's a quick solution based on that:
Copy and paste your lines into this page: https://codebeautify.org/javascript-escape-unescape
Then copy the escaped string, press F12 on a modern broser and paste the following code:
console.log(JSON.stringify("***PASTE HERE**".split("\n").map(x => ({text: x}))))
Then delete ***PASTE HERE*** and paste the escaped lines. Press enter and you'll have a JSON output similar to what you want.
I am using nodemailer to send mail from node server. I am getting the content for this mail from MSSQL SQL server which is formatted in plain text format, which meansa there are new line characters in it, however when I send it using nodemailer the newline characters are missing and the whole text looks messed up. The other way is to insert html tags for line break in the plain text and send this works fine. But there is too much mannual work involved what I am looking is for a library or utility which can convert the plain text into the html which I can send using mail.
Is there any liberary for this requirement or a way to do this automatically?
The following will wrap all parts that are separated by more than one newline in paragraphs (<p>...</p>) and insert breaks (<br>) where there is just one newline. A text block without any newlines will simply be wrapped in a paragraph.
template = '<p>' + template.replace(/\n{2,}/g, '</p><p>').replace(/\n/g, '<br>') + '</p>';
So for example, it will take this:
Title
First line.
Second line.
Footer
And convert it to this:
<p>Title</p><p>First line.<br>Second line.</p><p>Footer</p>
The simplest solution is you can replace the new line characters with <br>.
Try
text.split('\n').join('\n<br>\n')
then you are done.
Ok finally this code snippet worked for me -
template = template.replace(/\n/gi, "</p></br/>")
template = template.replace(/<\/p>/gi, "</p><p></br/>")
This was a lot of hit and trial but eventually it worked.
Im using mongodb to store some details. in this case, a testmess string.
testmess = "Angrep: /n beskyttelse: /n Intelligens";
and saving that do db.
im also retrieving it with <pre>{{ testmess }}</pre>
but what i get is:
Angrep: /n beskyttelse: /n Intelligens
why wont the <pre>work with it? same applies to <br>
It's exactly working as expected:
Whitespace inside this element [pre] is displayed as typed.
So it prints out what it get and does not interprets anything inside testmess.
To have a line break on your site, you have to have it in your testmess:
testmess = "Angrep:
beskyttelse:
Intelligens";
It seems not,as they are showing up as cleartext.
I am trying to format a rather large tootlip by inserting <p> and <br>, but, as I say, Chrome treats them as text.
What am I allowed to put into such a tooltip? Anything other than a single string?
Since \n seems to be ignored, is there any way to get a line break into such a string?
You can add symbol for a new line:
(\r) or
(\n) to your title.
test
Another option is to find some JavaScript tooltip library.
If you feed them actual line breaks, they will work.
<span title="Two
Liner">Hover here</span>
However, if you need more complex HTML inside, I'd suggest qTip or Bootstrap's tooltips
How would I escape space characters in a JSON string? Basically my problem is that I've gotten into a situation where the program that reads the string can use HTML tags for formatting, but I need to be able to use these HTML tags without adding more spaces to the string. so things like
<u>text</u>
is fine, for adding underline formatting
but something like
<font size="14">text</font>
is not fine, because the <font> tag with the size attribute adds an extra space to the string. I know, funny criteria, but at this point thats what has happened.
My first speculative solution would be to have some kind of \escape character that JSON can put in between font and size that will solve my "space" problems, something that the HTML will ignore but leave the human readable string in the code without actual spaces.
ex. <font\&size="14">text</font>
displays as: text
kind of like but better?
any solutions?
You can use \u0020 to escape the ' ' character in JSON.