Output UNIX environment as JSON

This works for me:

python -c 'import json, os;print(json.dumps(dict(os.environ)))'

It's pretty simple; the main complication is that os.environ is a dict-like object, but it is not actually a dict, so you have to convert it to a dict before you feed it to the json serializer.

Adding parentheses around the print statement lets it work in both Python 2 and 3, so it should work for the forseeable future on most *nix systems (especially since Python comes by default on any major distro).


Using jq 1.5 (e.g. jq 1.5rc2 -- see http://stedolan.github.io/jq):

$ jq -n env