Skip to content
Back to Blog

Storing multi-line strings in JSON with JSONLint

Rich Jones

Feb 9, 2017 10:45:00 AM

JSON is an extremely rigid schema. It's great, but it has a couple of shortcomings, the largest of which is the inability to store multi-line strings. This can be particularly annoying for storing things like structured text and public keys in JSON for later interaction with JavaScript code in the browser or on the server in Node.

Fortunately, there is a quick and hacky solution!


Imagine that we have a list of servers and their associated public keys. To store that data as JSON, we could store it as an array of comma-separated string objects, which might look something like this:

  "servers": {
  "": {
  "publicKey": [
  "-----BEGIN PUBLIC KEY-----",
  "-----END PUBLIC KEY-----"
view raw multiline.json hosted with ❤ by GitHub


You can verify that this valid JSON with JSONLint.

Then, to retrieve our text block, we just have to join them back together on the newline characters, which we can do like this:

  var publicKey = our_data.servers[''].publicKey.join('\n');
view raw multiline.js hosted with ❤ by GitHub


And that's it! Now you can store multiline strings in JSON. It's not the prettiest thing, but it is the simplest given the constraints of JSON.

Written By:

Rich Jones

Want to get our totally not sh*tty weekly newsletter?

Sometimes The Wayfarer is funny, sometimes insightful, but always the least spammy newsletter this side of Tatooine.