Skip to content

Instantly share code, notes, and snippets.

@TomKeur
Last active June 2, 2021 17:21
Show Gist options
  • Save TomKeur/b83925487183b425fb83558b07279607 to your computer and use it in GitHub Desktop.
Save TomKeur/b83925487183b425fb83558b07279607 to your computer and use it in GitHub Desktop.
DotEnv to JSON (useful for for HashiCorp Vault)
#!/usr/bin/env python
import json
import sys
try:
dotenv = sys.argv[1]
except IndexError as e:
dotenv = 'symfony-flex-environment.env'
with open(dotenv, 'r') as f:
content = f.readlines()
# Remove whitespace chars like "\n" at the end of each line
content = dict([x.strip().split('=', 1) for x in content if '=' in x])
# Remove items starting with a # and replace ' with nothing.
newdict = {}
for key, value in content.items():
if not key.startswith('#'):
newdict[key] = value.replace("'", '')
print(json.dumps(dict(newdict), indent=4, sort_keys=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment