I've JSON in the following format that I'm trying to insert into outlook/email client's body using
location.href = "mailto:" + "[email protected]" + "?subject=" + "Notes" + "&body=" + JSON.stringify(data, null, 4);
Giving 4 spaces JSON.stringify(data, null, 4)
[
{
"page": "article_0_page_0",
"note": "Note 1"
},
{
"page": "article_0_page_1",
"note": "Note 2"
}
]
I want to output as
<b>article_0_page_0</b>\n\r
Note 1
<b>article_0_page_1</b>\n\r
Note 2
Any regular expression solution please.
Edit: My attempt
var str = "";
for(var i=0; i<data.length; i++) {
str += "<strong>" + data[i].page + "</strong><br>" + data[i].note + "<br><br>";
}
I've JSON in the following format that I'm trying to insert into outlook/email client's body using
location.href = "mailto:" + "[email protected]" + "?subject=" + "Notes" + "&body=" + JSON.stringify(data, null, 4);
Giving 4 spaces JSON.stringify(data, null, 4)
[
{
"page": "article_0_page_0",
"note": "Note 1"
},
{
"page": "article_0_page_1",
"note": "Note 2"
}
]
I want to output as
<b>article_0_page_0</b>\n\r
Note 1
<b>article_0_page_1</b>\n\r
Note 2
Any regular expression solution please.
Edit: My attempt
var str = "";
for(var i=0; i<data.length; i++) {
str += "<strong>" + data[i].page + "</strong><br>" + data[i].note + "<br><br>";
}
Share
Improve this question
edited Nov 20, 2015 at 18:40
user2727195
asked Nov 20, 2015 at 18:26
user2727195user2727195
7,33020 gold badges73 silver badges121 bronze badges
7
- 1 Could you please describe what you have tried? – k0pernikus Commented Nov 20, 2015 at 18:29
- Could you provide us with what you've done so far? – Linus Oleander Commented Nov 20, 2015 at 18:29
- @k0pernikus You got me with 11 seconds :) – Linus Oleander Commented Nov 20, 2015 at 18:30
- @Oleander It would have been even faster, but for some reason "What did you try?" is no allowed content for a comment ;) – k0pernikus Commented Nov 20, 2015 at 18:31
- 1 @user2727195 Why not just change it according to your specs? You're almost done. – Linus Oleander Commented Nov 20, 2015 at 18:46
3 Answers
Reset to default 10I think the above answer is ok if you have a flat structure.
If you want a more holistic approach for email specifically, I have found this to work really well.
const data = {...}
JSON.stringify( data, null, ' ' ).split( '\n' ).join( '<br>' );
This will convert something like this
const json = {
"status": "ok",
"config": {
"gotIt": "yes",
"transform": [
null
]
}
}
Into something like this
{<br> "status": "ok",<br> "config": {<br>  "gotIt": "yes",<br>  "transform": [<br>   null<br>  ]<br> }<br>}
Email clients will render this as the following
{
"status": "ok",
"config": {
"gotIt": "yes",
"transform": [
null
]
}
}
This is done by using the additional parameters in JSON.stringify
which I don't see used very often if you want to read more about these parameters here is the link for them.
Here ya go
var json = [
{
"page": "article_0_page_0",
"note": "Note 1"
},
{
"page": "article_0_page_1",
"note": "Note 2"
}
];
var out = "";
for (var key in json) {
if (json.hasOwnProperty(key)) {
out += "<b>"+json[key].page+"</b>\r\n"+json[key].note+"\r\n\r\n";
}
}
console.log(out);
Example Here: https://jsfiddle.net/q5r4gdcn/1/
This is what I used to convert basic JSON to "readable" for email:
let json = {
"key": "asdas",
"query": "",
"hints": {
"nested1": "blahblah"
}
}
let prettyJSON = JSON.stringify(json, null, ' ')
.split('\n')
.join('<br> ')
.split('<br> }').join('<br>}')
document.body.innerHTML += `<div>${prettyJSON}</div>`