Skip to content

Instantly share code, notes, and snippets.

@omeranson
Created February 1, 2019 08:29
Show Gist options
  • Save omeranson/deef510e1694139e5c82cebb990f8eea to your computer and use it in GitHub Desktop.
Save omeranson/deef510e1694139e5c82cebb990f8eea to your computer and use it in GitHub Desktop.
Utility to convert python values to json (Uses `eval`. Use only on trusted data!)
#!/bin/env python3
import json
import sys
def get_streams():
if len(sys.argv) == 1:
yield sys.stdin
return
for arg in sys.argv[1:]:
if arg == "-":
yield sys.stdin
continue
with open(arg, "r") as f:
yield f
def main():
for s in get_streams():
d = eval(s.read())
print(json.dumps(d))
if __name__ == "__main__":
try:
main()
sys.exit(0)
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment