How to Store JSON in an Environment Variable

Store JSON in an Environment Variable

I recently ran into a problem that compelled me to store some configuration data in a JSON object. The data wasn’t likely to change very often, but I didn’t want to hard code it in the application code. Unfortunately, there was not a good external data store immediately available to me for this kind of use case.

Ultimate Guitar PDFs

The search for a good solution led me to an idea. Could I store JSON in an environment variable? It turns out I can. I’m still not convinced it is the best solution, but it certainly has been a useful one. It at least allows me to change the configuration data without changing the code artifact. If you’ve worked in a web development or devops role long enough you’ll recognize the value of this.

The first problem I ran into was the raw JSON data breaking the config file. The JSON object has lots of special characters in it. These characters caused things to break when I tried to set the environment variable on the Linux server to the raw JSON string.

After a bit of thought I came up with the obvious solution: get rid of those stupid characters. Unfortunately, those characters are kind of important. I couldn’t just remove them.

What I could do was represent the data differently. I minified the JSON block and then base64 encoded it. That took the data from looking like this:

{
   "person": "you",
   "action": "subscribe",
   "when": "now",
   "happy": true
}

To looking like this:

{"person":"you","action":"subscribe","when":"now","happy":true}

And then finally this:

eyJwZXJzb24iOiJ5b3UiLCJhY3Rpb24iOiJzdWJzY3JpYmUiLCJ3aGVuIjoibm93IiwiaGFwcHkiOnRydWV9

When the JSON data only consists of alphanumeric characters it becomes a lot easier to work with. My application code can now read it from the environment variable and base64 decode it on the other side.

I thought I’d share this pattern in case it becomes useful for anybody else. In the past I’ve also used base64 encoding to pass image byte data across the internet. It’s a handy little technique with various applications. You do have to be careful with the size of the data though.

Now, go leave me a comment telling me why storing JSON in an environment variable is a horrible idea and subscribe so you can offer similar criticism in the future.