Skip to content
Back to Blog

JSON Tutorial: The JSON format for storing multi-line strings & JSONLint

Rich Jones

Feb 9, 2017 10:45:00 AM

JSON is an extremely rigid schema. It's great when you’re trying to learn JSON, 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 to this JSON data and coding conundrum!

JSON multi-strings examples

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 JSON file example:

  "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.

Retrieving the JSON language to JSON new line

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 and get results in JSON single line. 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.