Skip to content

Instantly share code, notes, and snippets.

@kesor
Last active January 22, 2019 17:39
Show Gist options
  • Save kesor/4f80895bdb922d964f9004f3ddd019c7 to your computer and use it in GitHub Desktop.
Save kesor/4f80895bdb922d964f9004f3ddd019c7 to your computer and use it in GitHub Desktop.
Convert CloudFormation YAML format to JSON format
#!/usr/bin/env python
import sys, yaml, json
def funcparse(loader, node):
node.value = {
yaml.constructor.ScalarNode: loader.construct_scalar,
yaml.constructor.SequenceNode: loader.construct_sequence,
yaml.constructor.MappingNode: loader.construct_mapping,
}[type(node)](node)
node.tag = node.tag.replace(u'!Ref', 'Ref').replace(u'!', u'Fn::')
return dict([ (node.tag, node.value) ])
funcnames = [ 'Ref', 'Base64', 'FindInMap', 'GetAtt', 'GetAZs', 'ImportValue',
'Join', 'Select', 'Split', 'Split', 'Sub', 'And', 'Equals', 'If',
'Not', 'Or' ]
for func in funcnames:
yaml.SafeLoader.add_constructor(u'!' + func, funcparse)
json.dump(yaml.safe_load(sys.stdin), sys.stdout, indent=4)
@chadawagner
Copy link

Thank you!

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