Python

yaml to json

Kyle79 2020. 4. 2. 14:05

 

The PyYAML library is intended for this purpose

 

pip install pyyaml
import yaml
import json
with open("example.yaml", 'r') as yaml_in, open("example.json", "w") as json_out:
    yaml_object = yaml.safe_load(yaml_in) # yaml_object will be a list or a dict
    json.dump(yaml_object, json_out)

Notes: PyYAML only supports the pre-2009, YAML 1.1 specification.
ruamel.yaml is an option if YAML 1.2 is required.

pip install ruamel.yaml
import ruamel.yaml
import json

in_file = 'input.yaml'
out_file = 'output.json'

yaml = ruamel.yaml.YAML(typ='safe')
with open(in_file) as fpi:
    data = yaml.load(fpi)
with open(out_file, 'w') as fpo:
    json.dump(data, fpo, indent=2)

'Python' 카테고리의 다른 글

s3 upload(stream, smart_open), download (pre-signed url, csv/json, bucket policy, credentials)  (0) 2020.06.10
flask proxy server  (0) 2020.04.20
Celery  (0) 2020.01.13
Python Sklearn's [joblib] library  (0) 2019.12.19
Asterisk(*)  (0) 2019.12.02