Import flip, to_yaml, or to_json from cfn_flip. All functions expect a string containing serialized data and return a string containing serialized data. They raise an exception if parsing fails.
flip(data): Detects format and returns the opposite (JSON $\leftrightarrow$ YAML).to_json(yaml_string): Converts YAML input to JSON output.to_yaml(json_string, clean_up=False): Converts JSON input to YAML output.
All three functions support the clean_up parameter, which performs opinionated CloudFormation-specific sanitation (e.g., converting Fn::Join to Fn::Sub).
from cfn_flip import flip, to_yaml, to_json
# Automatically flip format
some_yaml_or_json = flip(some_json_or_yaml)
# Explicitly convert to JSON
some_json = to_json(some_yaml)
# Explicitly convert to YAML with cleanup
clean_yaml = to_yaml(some_json, clean_up=True)