最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Formatting JSON for email body - Stack Overflow

programmeradmin3浏览0评论

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
 |  Show 2 more comments

3 Answers 3

Reset to default 10

I 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, '&nbsp;' ).split( '\n' ).join( '<br>' );

This will convert something like this

const json = {
  "status": "ok",
   "config": {
     "gotIt": "yes",
     "transform": [
       null
     ]
   }
}

Into something like this

{<br>&nbsp"status": "ok",<br>&nbsp"config": {<br>&nbsp&nbsp"gotIt": "yes",<br>&nbsp&nbsp"transform": [<br>&nbsp&nbsp&nbspnull<br>&nbsp&nbsp]<br>&nbsp}<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, '&nbsp;')
  .split('\n')
  .join('<br>&nbsp;&nbsp;')
  .split('<br>&nbsp;&nbsp;}').join('<br>}')

document.body.innerHTML += `<div>${prettyJSON}</div>`

发布评论

评论列表(0)

  1. 暂无评论