Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert JSON to YAML
#!/bin/sh
export PYTHONPATH=$HOME/usr/opt/pyyaml/lib/python2.6/site-packages
python -c '
import sys, json, yaml
with open(sys.argv[1]) as f:
print yaml.safe_dump(json.load(f), default_flow_style=False)
' $@
@tarunlalwani
Copy link

tarunlalwani commented Mar 8, 2016

@goldyfruit, it is correct. Loads is for strings and load is for file pointer

@shdobxr
Copy link

shdobxr commented Nov 18, 2016

Why not just leave it in python?

`#!/usr/local/bin/python

import sys, json, yaml

with open(sys.argv[1]) as f:
print yaml.safe_dump(json.load(f), default_flow_style=False)`

@azizzoaib786
Copy link

azizzoaib786 commented Sep 4, 2018

print yaml.safe_dump(json.loads(f), default_flow_style=False)
^
SyntaxError: invalid syntax

@ausarb
Copy link

ausarb commented Nov 15, 2018

That syntax error is a Python 3 thing, where print is now a function. https://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-function

print(yaml.safe_dump(json.load(f), default_flow_style=False))

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