Last active
January 22, 2019 17:39
-
-
Save kesor/4f80895bdb922d964f9004f3ddd019c7 to your computer and use it in GitHub Desktop.
Convert CloudFormation YAML format to JSON format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!