Skip to content

Instantly share code, notes, and snippets.

@noahcoad
Last active December 27, 2019 22:46
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save noahcoad/51934724e0896184a2340217b383af73 to your computer and use it in GitHub Desktop.
Save noahcoad/51934724e0896184a2340217b383af73 to your computer and use it in GitHub Desktop.
Python to Convert yaml to json
# convert yaml to json
# pip3 install pyyaml
# http://pyyaml.org/wiki/PyYAMLDocumentation
# py3 yaml2json.py < ~/code/manpow/homeland/heartland/puphpet/config.yaml
# gist https://gist.github.com/noahcoad/51934724e0896184a2340217b383af73
import yaml, json, sys
sys.stdout.write(json.dumps(yaml.load(sys.stdin), sort_keys=True, indent=2))
@jean-christophe-manciot
Copy link

Hi there,
I tried it on the attached yaml file; I got the following issue:

# python3 /home/actionmystique/Ansible/yaml2json.py < interfaces.yml
Traceback (most recent call last):
  File "/home/actionmystique/Ansible/yaml2json.py", line 8, in <module>
    sys.stdout.write(json.dumps(yaml.load(sys.stdin), sort_keys=True, indent=2))
  File "/usr/local/lib/python3.5/dist-packages/yaml/__init__.py", line 72, in load
    return loader.get_single_data()
  File "/usr/local/lib/python3.5/dist-packages/yaml/constructor.py", line 35, in get_single_data
    node = self.get_single_node()
  File "/usr/local/lib/python3.5/dist-packages/yaml/composer.py", line 36, in get_single_node
    document = self.compose_document()
  File "/usr/local/lib/python3.5/dist-packages/yaml/composer.py", line 55, in compose_document
    node = self.compose_node(None, None)
  File "/usr/local/lib/python3.5/dist-packages/yaml/composer.py", line 84, in compose_node
    node = self.compose_mapping_node(anchor)
  File "/usr/local/lib/python3.5/dist-packages/yaml/composer.py", line 133, in compose_mapping_node
    item_value = self.compose_node(node, item_key)
  File "/usr/local/lib/python3.5/dist-packages/yaml/composer.py", line 84, in compose_node
    node = self.compose_mapping_node(anchor)
  File "/usr/local/lib/python3.5/dist-packages/yaml/composer.py", line 133, in compose_mapping_node
    item_value = self.compose_node(node, item_key)
  File "/usr/local/lib/python3.5/dist-packages/yaml/composer.py", line 84, in compose_node
    node = self.compose_mapping_node(anchor)
  File "/usr/local/lib/python3.5/dist-packages/yaml/composer.py", line 127, in compose_mapping_node
    while not self.check_event(MappingEndEvent):
  File "/usr/local/lib/python3.5/dist-packages/yaml/parser.py", line 98, in check_event
    self.current_event = self.state()
  File "/usr/local/lib/python3.5/dist-packages/yaml/parser.py", line 428, in parse_block_mapping_key
    if self.check_token(KeyToken):
  File "/usr/local/lib/python3.5/dist-packages/yaml/scanner.py", line 116, in check_token
    self.fetch_more_tokens()
  File "/usr/local/lib/python3.5/dist-packages/yaml/scanner.py", line 220, in fetch_more_tokens
    return self.fetch_value()
  File "/usr/local/lib/python3.5/dist-packages/yaml/scanner.py", line 576, in fetch_value
    self.get_mark())
yaml.scanner.ScannerError: mapping values are not allowed here
  in "<stdin>", line 31, column 13

interfaces.yml

openconfig-interfaces:
  interfaces:
    interface:
      mgmt
        name: 0           # string
        config:
          name: 0           # string
          type: ethernet-csmacd          
                                # softwareLoopback || ethernet-csmacd || ieee8023adLag || 
                                # l2vlan (L2 802.1Q) || l3ipvlan (L3 802.1Q)
                                # https://www.iana.org/assignments/smi-numbers/smi-numbers.xhtml#smi-numbers-5
          mtu:                    # uint16
          description: "Management interface"
          enabled: true           # boolean
        ethernet:
          config:
            mac_address:            # E.E.E || EE-EE-EE-EE-EE-EE || EE:EE:EE:EE:EE:EE || EEEE.EEEE.EEEE
            auto_negotiate: true    # boolean
            duplex_mode:            # FULL || HALF
            port_speed:             # SPEED_10MB || SPEED_100MB || SPEED_1GBB || SPEED_2_5GB || SPEED_5GB ||
                                  # SPEED_10GB || SPEED_25GB || SPEED_40GB || SPEED_50GB || SPEED_100GB ||
                                  # SPEED_UNKNOWN
            enable_flow_control:    # boolean
            aggregate-id:           # -> /oc-if:interfaces/interface/name
...

@JoelFeiner
Copy link

You left out a colon after mgmt on the 4th line.

@ravibhure
Copy link

better to use YAMLlint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment