Skip to content

Instantly share code, notes, and snippets.

@sonnyksimon
Created October 1, 2023 07:26
Show Gist options
  • Save sonnyksimon/bf0fc5350bfb238bf03766e1ada5e034 to your computer and use it in GitHub Desktop.
Save sonnyksimon/bf0fc5350bfb238bf03766e1ada5e034 to your computer and use it in GitHub Desktop.
yaml2json: Compact script for yaml to json conversion
#!/usr/bin/env python
"""yaml2json: Compact script for yaml to json conversion"""
import yaml
import json
import sys
args = [open(x,'r') for x in sys.argv[1:]]
args = [sys.stdin.read(), *args] if not sys.stdin.isatty() else args
files = [yaml.safe_load(y) for y in args]
out = files[0] if len(files) == 1 else files
sys.stdout.write(json.dumps(out,separators=(',',':')))
@sonnyksimon
Copy link
Author

cat example.yml | yaml2json example2.yml example3.yml

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