Sending a JSON message in body text of email, strings broken in midstream

I wanted people to be able to grab a section of JSON out of a webpage and email it to someone. This seems to not be possible, since email systems (e.g. gmail) will break up the lines, possibly breaking a string constant in the middle...which makes it not possible to parse back.

I'm looking for a minimal way to transform the JSON so that it is still readable and recognizable in the email payload, and then do an equally minimal transformation back. That means something like UUENCODING is not a fit; I want it to be possible to look at it and go "yup, that's JSON... mostly."

Seems I'm up against a little bit of a wall. I could make an assumption about how long lines are allowed to be and put in some kind of line-break character:

{
    foo: [10 20],
    bar: "some really long string that the email would break\
and we handle the discontinuity with backslash or something"
}

(UPDATE: even better is probably this trick which passes strings as an array of strings, and then joins upon receipt, but that's a bit more invasive than I was hoping to be)

If I don't want to set a known line length limit, I'd have to assume whitespace can't be significant. Perhaps some post-processing step on the sending side to look inside of strings and replace the spaces with \x20, and some pre-processing step on the reading side to collapse all literal spaces outside of strings to a single space and all literal spaces inside of strings go away entirely? :-/

(The only whitespace in the strings for my data set are space and newline, incidentally.)

Overall, I'm wondering if anyone can think of an easy general hack to make a JSON message keep working in a medium that just throws line breaks in when it feels like it. Less code the better!

Upvotes: 3

Views: 15501

Answers (1)

Patrick Evans
Patrick Evans

Reputation: 42736

if your email client can do html emails you can wrap the code in <pre></pre> tags

Seems to work when sending a html email to my gmail account of json. The json text will look like it breaks but when you go and copy and paste the json into like an editor or even the console it pastes correctly and can be executed. Least it did when I tested using Chrome with GMail as the email client.

Emailed with <pre></pre> tags enter image description here

Alternatively you can wrap the json in <div></div> or whatever tag and set an inline style of white-space:nowrap; which will put the whole json on one unbreaking line.

Emailed using white-space:nowrap enter image description here

Of course this is dependent on the email client's capabilities like supporting html and/or css.

Upvotes: 8

Related Questions