Converting a YAML file to Python JSON object

convert json to yaml python
yaml to json python online
python yaml
convert yaml to json bash
python object to yaml
python append to yaml file
python object to json
yaml boolean python

How can I load a YAML file and convert it to a Python JSON object?

My YAML file looks like this:

Section:
    heading: Heading 1
    font: 
        name: Times New Roman
        size: 22
        color_theme: ACCENT_2

SubSection:
    heading: Heading 3
    font:
        name: Times New Roman
        size: 15
        color_theme: ACCENT_2
Paragraph:
    font:
        name: Times New Roman
        size: 11
        color_theme: ACCENT_2
Table:
    style: MediumGrid3-Accent2

you can use PyYAML

pip install PyYAML

And in the ipython console:

In [1]: import yaml

In [2]: document = """Section:
   ...:     heading: Heading 1
   ...:     font: 
   ...:         name: Times New Roman
   ...:         size: 22
   ...:         color_theme: ACCENT_2
   ...: 
   ...: SubSection:
   ...:     heading: Heading 3
   ...:     font:
   ...:         name: Times New Roman
   ...:         size: 15
   ...:         color_theme: ACCENT_2
   ...: Paragraph:
   ...:     font:
   ...:         name: Times New Roman
   ...:         size: 11
   ...:         color_theme: ACCENT_2
   ...: Table:
   ...:     style: MediumGrid3-Accent2"""
   ...:     

In [3]: yaml.load(document)
Out[3]: 
{'Paragraph': {'font': {'color_theme': 'ACCENT_2',
   'name': 'Times New Roman',
   'size': 11}},
 'Section': {'font': {'color_theme': 'ACCENT_2',
   'name': 'Times New Roman',
   'size': 22},
  'heading': 'Heading 1'},
 'SubSection': {'font': {'color_theme': 'ACCENT_2',
   'name': 'Times New Roman',
   'size': 15},
  'heading': 'Heading 3'},
 'Table': {'style': 'MediumGrid3-Accent2'}}

Transform YAML into JSON, as the previous result to parse it, returning a dictionary. If it uses the newer YAML 1.2 specification, which made YAML into a superset of JSON, you should use ruamel.yaml (disclaimer: I am the author of that package, which is a fork of PyYAML).

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

Reading and Writing JSON to a File in Python, Python Object Serialization: yaml & json. YAML has the ability to reference other items within a YAML file using Let's convert the following json to yaml: json.dump(s) and json.load(s) Python Object Serialization - pickle and json Python Object Serialization - yaml and json Priority queue and heap queue data structure Graph data structure Dijkstra's shortest path algorithm Prim's spanning tree algorithm Closure Functional programming in Python Remote running a local file using ssh

In python3 you can use pyyaml.

$ pip3 install pyyaml

Then you load your yaml file and dump it into json:

import yaml, json

with open('./file.yaml') as f:
    print(json.dumps(yaml.load(f)))

Output:

{"Section": null, "heading": "Heading 1", "font": {"name": "Times New Roman", "size": 22, "color_theme": "ACCENT_2"}, "SubSection": {"heading": "Heading 3", "font": {"name": "Times New Roman", "size": 15, "color_theme": "ACCENT_2"}}, "Paragraph": {"font": {"name": "Times New Roman", "size": 11, "color_theme": "ACCENT_2"}}, "Table": {"style": "MediumGrid3-Accent2"}}

How to parse and extract data from a YAML file in Python, Python to Convert yaml to json. import yaml, json, sys self.current_event = self.state() File "/usr/local/lib/python3.5/dist-packages/yaml/parser.py", line 428,  Python one-liner: converting JSON to YAML Posted on October 11, 2015 by jasoncallaway I’ve been playing with the Titan graph database lately; it’s hella cool, super powerful, and has a great ecosystem.

There is no such thing as a Python JSON object. JSON is a language independent file format that finds its roots in JavaScript, and is supported by many languages.

If your YAML document adheres to the old 1.1 standard, i.e. pre-2009, you can use PyYAML as suggested by some of the other answers.

If it uses the newer YAML 1.2 specification, which made YAML into a superset of JSON, you should use ruamel.yaml (disclaimer: I am the author of that package, which is a fork of PyYAML).

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)

which generates output.json:

{
  "Section": {
    "heading": "Heading 1",
    "font": {
      "name": "Times New Roman",
      "size": 22,
      "color_theme": "ACCENT_2"
    }
  },
  "SubSection": {
    "heading": "Heading 3",
    "font": {
      "name": "Times New Roman",
      "size": 15,
      "color_theme": "ACCENT_2"
    }
  },
  "Paragraph": {
    "font": {
      "name": "Times New Roman",
      "size": 11,
      "color_theme": "ACCENT_2"
    }
  },
  "Table": {
    "style": "MediumGrid3-Accent2"
  }
}

ruamel.yaml, apart from supporting YAML 1.2, has many PyYAML bugs fixed. You should also note that PyYAML's load() is also documented to be unsafe, if you don't have full control over the input at all times. PyYAML also loads scalar numbers 021 as integer 17 instead of 21 and converts scalar strings like on, yes, off to boolean values (resp. True, True and False).

Python Object Serialization: yaml & json - 2020, python -c 'import sys, yaml, json; y=yaml.load(sys.stdin.read()); print recent call last): File "<string>", line 1, in <module> ImportError: No module named yaml. I've recently been finding myself trying to coerce YAML to JSON and vice versa quite a bit, partly to convert attributes from a Test Kitchen YAML file to a nice JSON object that can be consumed by Vagrant's Chef provisioner. As it's been required a number of times, I decided that I needed to script it.

Python to Convert yaml to json · GitHub, This YAML to JSON converter transforms YAML (Yet Another Markup Language) configs and files into JSON (JavaScript Object Notation) configs and files. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

YAML to JSON one-liner · GitHub, Converts YAML input to JSON output. Relies on PyYaml, which supports YAML 1.0 and 1.1, but not 1.2. yml2json has been tested on Python 2.7. The output will display below the Convert button. See also JSON to YAML Option 1 - Choose a YAML file Encoding Option 2 - Enter an URL Option 3 - paste into Text Box below

yml2json · PyPI, Kitchen YAML file to a nice JSON object that can be consumed by Vagrant's Chef provisioner. with Python's JSON module: ytoj < file.yml | python -m json.​tool . To convert from YAML to JSON, we can use the following:. I want to use Python to convert JSON data into a Python object. I receive JSON data objects from the Facebook API, which I want to store in my database. My current View in Django (Python) (requ

Comments
  • Not only what the previous comment said, but you're using IPython's console, and not plain Python console ;)
  • 1) Where is the JSON the OP requested? JSON strings have double quotes. 2) PyYAML's load() is documented to be unsafe, and there is no excuse for using it instead of safe_load() here (or almost anywhere else). 3) You fail to mention that PyYAML only supports the old, pre-2009, YAML 1.1 specification.
  • 1. what did you mean about the JSON type in Python, may be you can help me to read about it. is the dict. Other comments is good and interesting as your answer, thank you.
  • I agree, it's a more clear answer. I'll leave my answer here since it includes the file handling part, although it was not asked specifically for it is probably needed more often than not.
  • I copied the pip install part when you mentioned the other answer being cleared, thanks.
  • PyYAML's load() is documented to be unsafe, and there is no excuse for using it instead of safe_load() here (or almost anywhere else). You fail to mention that PyYAML only supports the old, pre-2009, YAML 1.1 specification.
  • I had no idea, I'll include it in the answer, thanks.
  • PyYAML's load() is documented to be unsafe, and there is no excuse for using it instead of safe_load() here (or almost anywhere else). Like many others you fail to mention that PyYAML only supports the old, pre-2009, YAML 1.1 specification.