How do I work with instances of YANG data?

This document contains some examples of encoding and decoding yang data. To perform these operations, the CodecService is used.

The below approaches can be used to perform encoding and decoding of an interface Ipv4 loopback configuration. For these examples, the Cisco_IOS_XR_ifmgr_cfg.InterfaceConfigurations class is used. Note that the ydk and ydk-models-cisco-ios-xr python packages need to be installed for this example.

Converting between JSON and XML

To parse a JSON string representing yang data into a YDK python object and then to an XML string, the below approach can be used.

 1from ydk.providers import CodecServiceProvider
 2from ydk.services import CodecService
 3
 4# Instantiate the codec service
 5codec = CodecService()
 6
 7# Instantiate codec providers with json and xml options
 8json_provider = CodecServiceProvider(type='json')
 9xml_provider = CodecServiceProvider(type='xml')
10
11# Declare the JSON configuration
12if_json = ''' {
13  "Cisco-IOS-XR-ifmgr-cfg:interface-configurations": {
14    "interface-configuration": [
15      {
16        "active": "act",
17        "interface-name": "Loopback0",
18        "description": "PRIMARY ROUTER LOOPBACK",
19        "Cisco-IOS-XR-ipv4-io-cfg:ipv4-network": {
20          "addresses": {
21            "primary": {
22              "address": "172.16.255.1",
23              "netmask": "255.255.255.255"
24            }
25          }
26        }
27      }
28    ]
29  }
30}
31'''
32
33# Invoke the decode method  to decode the JSON payload to a YDK python object
34interface_configurations = codec.decode(json_provider, if_json)
35
36# Invoke the encode method to encode the YDK python object to an XML string
37if_xml = codec.encode(xml_provider, interface_configurations)
38print(if_xml)

Converting to JSON

To convert a YDK python object into a JSON string, the below approach can be used. Note that the attribute primary is an instance of a presence class, which is set to None by default. So it needs to be assigned to a new instance of its class.

 1from ydk.providers import CodecServiceProvider
 2from ydk.services import CodecService
 3from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ifmgr_cfg
 4
 5# Instantiate the codec service
 6codec = CodecService()
 7
 8# Instantiate the provider with json option
 9json_provider = CodecServiceProvider(type='json')
10
11# Instantiate the interface configuration class to configure the IPv4 loopback
12interface_configurations =  Cisco_IOS_XR_ifmgr_cfg.InterfaceConfigurations()
13
14# Instantiate the InterfaceConfiguration list instance
15interface_configuration = interface_configurations.InterfaceConfiguration()
16interface_configuration.active = "act"
17interface_configuration.interface_name = "Loopback0"
18interface_configuration.description = "PRIMARY ROUTER LOOPBACK"
19
20# Instantiate the Primary presence node
21interface_configuration.ipv4_network.addresses.primary = interface_configuration.ipv4_network.addresses.Primary()
22interface_configuration.ipv4_network.addresses.primary.address = "172.16.255.1"
23interface_configuration.ipv4_network.addresses.primary.netmask = "255.255.255.255"
24
25# Append the list instance to the parent list
26interface_configurations.interface_configuration.append(interface_configuration)
27
28# Invoke the encode method to encode the YDK python object to a JSON payload
29json = codec.encode(json_provider, interface_configurations)
30print(json)