def flip(template, in_format=None, out_format=None, clean_up=False, no_flip=False, long_form=False): """ Figure out the input format and convert the data to the opposing output format """ # Do we need to figure out the input format? if not in_format: # Load the template as JSON? if (out_format == "json" and no_flip) or (out_format == "yaml" and not no_flip): in_format = "json" elif (out_format == "yaml" and no_flip) or (out_format == "json" and not no_flip): in_format = "yaml" # Load the data if in_format == "json": data = load_json(template) elif in_format == "yaml": data = load_yaml(template) else: data, in_format = load(template) # Clean up? if clean_up: data = clean(data) # Figure out the output format if not out_format: if (in_format == "json" and no_flip) or (in_format == "yaml" and not no_flip): out_format = "json" else: out_format = "yaml" # Finished! if out_format == "json": if sys.version[0] == "3": return dump_json(data) else: return dump_json(data).encode('utf-8') return dump_yaml(data, clean_up, long_form)
def yaml_to_json(p_input_file): json_out = ROOT_DIR+'/../../../tacocat_cf.json' with open( p_input_file,'r') as yaml_in: yaml_data = load_yaml(yaml_in) yaml_data = clean(yaml_data) with open(json_out,'w') as out_file: json_data = dump_json(yaml_data) json_data = load_json(json_data) json.dump(json_data ,out_file, sort_keys=True, indent=4, separators=(',', ': ')) print('*--*--*--*--*--*--*--*--*--*--*--*--**--*--*--*--*--*--*--*--*--*--*--*') print('The new JSON file has been created in the root folder of the project with the name tacocat_cf.json') print('*--*--*--*--*--*--*--*--*--*--*--*--**--*--*--*--*--*--*--*--*--*--*--*')
def convert_yaml_to_json(yaml_file): converted_file = ROOT_DIR + '/../../../tacocat_cf.json' with open(yaml_file, 'r') as file_in: yaml_data = load_yaml(file_in) yaml_data = clean(yaml_data) with open(converted_file, 'w') as file_out: json_data = dump_json(yaml_data) json_data = load_json(json_data) json.dump(json_data, file_out, sort_keys=True, indent=4, separators=(',', ': ')) print( '#####--Converted successfully, please check the root folder with the name tacocat_cf.json--#####' )