3

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!

Community
  • 1
  • 1

1 Answers1

7

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.

Patrick Evans
  • 41,991
  • 6
  • 74
  • 87
  • Interesting...I did not realize it would just "guess" at content being html. That's good to know, though it sounds a little brittle; depends on the properties of the mail client, I guess? – HostileFork says dont trust SE Mar 18 '14 at 02:08
  • Actually it didn't guess at the content being html, it had to be sent with the appropriate headers eg: `Content-Type: text/html; charset=ISO-8859-1` in the mail function (i used php to send the email, [link](http://css-tricks.com/sending-nice-html-email-with-php/)), otherwise it would have just been sent as text. But yea it is all dependent on capabilities of the mail client. – Patrick Evans Mar 18 '14 at 02:16
  • I laid out in the question that it pretty much wasn't possible to do...and besides being the only answer I received, your answer is the only one that works around the impossibility. However, I chose to bite the bullet and use Base64. This does raise the question of human-readable formats that can survive arbitrary line breaks yet still encode strings... a potentially interesting question in its own right? JSON isn't it, though. – HostileFork says dont trust SE Apr 30 '14 at 01:57