Skip to content

Instantly share code, notes, and snippets.

@kr
Created August 6, 2013 00:59
Show Gist options
  • Select an option

  • Save kr/6161118 to your computer and use it in GitHub Desktop.

Select an option

Save kr/6161118 to your computer and use it in GitHub Desktop.
convert a json dictionary into environment variables
#!/usr/bin/env python
# jsonenv reads a json object as input and produces
# escaped shell commands for setting environment vars
import json
import pipes
import sys
for k, v in json.load(sys.stdin).items():
k = pipes.quote(k)
v = pipes.quote(v)
print "%s=%s export %s;" % (k, v, k)
@kr

kr commented Aug 6, 2013

Copy link
Copy Markdown
Author

Given x.json:

{
  "URL": "https://foo:bar@example.com/",
  "OTHER": "foo \" bar 'baz'"
}

This produces:

$ ./jsonenv <x.json 
URL=https://foo:bar@example.com/ export URL;
OTHER='foo " bar '"'"'baz'"'"'' export OTHER;

You can use it with the heroku api like this:

$ eval "$(curl -sn https://api.heroku.com/apps/myapp/config_vars|jsonenv)"

to use the config vars from myapp in your local shell.

@sprive

sprive commented Apr 3, 2015

Copy link
Copy Markdown

This is useful, especially if gradually migrating code between Bash and Python.
Heads up though, this gist will only work with strings: not bool, int, or array.

@thapakazi

Copy link
Copy Markdown

thanks mate 😄

@brendanmaguire

Copy link
Copy Markdown

Very nice. Thanks!

@zored

zored commented May 26, 2017

Copy link
Copy Markdown

@kr why not to export URL=...?

@amussey

amussey commented Sep 26, 2017

Copy link
Copy Markdown

If you're attempting to use this with python3, all you need to do is change the line:

print "%s=%s export %s;" % (k, v, k)

to:

print("%s=%s export %s;" % (k, v, k))

Thanks @kr!

@neps-in

neps-in commented Sep 27, 2017

Copy link
Copy Markdown

Thanks, can you help me how do i do the reverse way, convert bash config separated by = to give json string.
conf1='value1'
conf2='value2'

to produce json string
output={
'conf1':'value1',
'conf2':'value2'
}

@tirupathiraop

Copy link
Copy Markdown

@neps-in
did you figure out any way to do it?

@jakub-bochenski

Copy link
Copy Markdown

pipes.quote is wrong for the env names. Env names can only contain word characters, so it should be instead sth like k = re.sub("[^a-zA-Z_0-9]+", "_", k)

@cfraizer

Copy link
Copy Markdown

Have you tried jq as an alternative?

$ jq -r '. | to_entries[] | "export \(.key)=\(@sh "\(.value)")"' ~/tmp/x.json
export URL='https://foo:bar@example.com/'
export OTHER='foo " bar '\''baz'\'''

That's not identical output to the Python above, but it is equivalent. (To encode single-quote (&apos;) characters, it uses <backslash>&apos; outside quotes instead of &quot;&apos;&quot;.)

@brianjychan

Copy link
Copy Markdown

Thanks

@jankuc

jankuc commented Mar 31, 2021

Copy link
Copy Markdown

this should work without the need to paste code into cmdline again:

for k, v in json.load(open('local.settings.json')).items():
    os.environ[k] = v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment