<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Converting from XML to JSON (YAML) via YDK in Tools</title>
    <link>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4572764#M1527</link>
    <description>&lt;P&gt;Thank you for detailed informations. This code below was solved my problem too&lt;A href="https://serviceproviderslist.com/what-is-tailgating-in-cyber-security/" target="_self"&gt;.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;with open(xml_file, "rb") as f:
    config_xml = f(read)&lt;BR /&gt;decoded_entity = codec.decode(XML_PROVIDER, config_xml)&lt;BR /&gt;config_json = codec.encode(JSON_PROVIDER, decoded_entity)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 17 Mar 2022 16:01:00 GMT</pubDate>
    <dc:creator>ozdenkarabulut</dc:creator>
    <dc:date>2022-03-17T16:01:00Z</dc:date>
    <item>
      <title>Converting from XML to JSON (YAML) via YDK</title>
      <link>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4572688#M1525</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to convert a Juniper vendor-specific XML configuration using CodecService encode and decode API. Juniper yang models have been generated and installed however the code is failing with an error:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Traceback (most recent call last):
  File "convert.py", line 69, in &amp;lt;module&amp;gt;
    print(yang_xml_to_json(ARGS['xml_file']))
  File "convert.py", line 58, in yang_xml_to_json
    decoded_xml_yang = CODEC.decode(XML_PROVIDER, config_xml_yang)
  File "/opt/neteng/lib/python3.6/site-packages/ydk/errors/error_handler.py", line 112, in helper
    return func(self, provider, entity, *args, **kwargs)
  File "/opt/neteng/lib/python3.6/site-packages/ydk/services/codec_service.py", line 130, in decode
    entity = self.decode(provider, payload_holder[key], subtree)
  File "/opt/neteng/lib/python3.6/site-packages/ydk/errors/error_handler.py", line 112, in helper
    return func(self, provider, entity, *args, **kwargs)
  File "/opt/neteng/lib/python3.6/site-packages/ydk/services/codec_service.py", line 130, in decode
    entity = self.decode(provider, payload_holder[key], subtree)
  File "/opt/neteng/lib/python3.6/site-packages/ydk/errors/error_handler.py", line 112, in helper
    return func(self, provider, entity, *args, **kwargs)
  File "/opt/neteng/lib/python3.6/site-packages/ydk/services/codec_service.py", line 140, in decode
    return self._decode(provider, payload_holder, subtree)
  File "/opt/neteng/lib/python3.6/site-packages/ydk/services/codec_service.py", line 158, in _decode
    entity = _payload_to_top_entity(payload, provider.encoding)
  File "/opt/neteng/lib/python3.6/site-packages/ydk/entity_utils/entity_utils.py", line 117, in _payload_to_top_entity
    raise YModelError("Could not retrieve namespace and container name")
ydk.errors.YModelError: Could not retrieve namespace and container name&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;pip freeze | grep ydk
ydk==0.8.4
ydk-models-ietf==0.1.5.post2
ydk-models-junos-18-4==18.4.post3
ydk-models-openconfig==0.1.8&lt;/PRE&gt;&lt;P&gt;Code is the following:&lt;/P&gt;&lt;PRE&gt;import argparse
import json
import yaml
import xmltodict

from ydk.providers import CodecServiceProvider
from ydk.services import CodecService


codec = CodecService()

JSON_PROVIDER = CodecServiceProvider(type='json')
XML_PROVIDER = CodecServiceProvider(type='xml')

def yang_xml_to_json (xml_file):
    with open(xml_file, "rb") as f:
        config_xml_yang = xmltodict.parse(f, xml_attribs=True)

    decoded_xml_yang = codec.decode(XML_PROVIDER, config_xml_yang)
    yang_json = code.encode(JSON_PROVIDER, decoded_xml_yang)
    return yang_json&lt;/PRE&gt;&lt;P&gt;I am using the XML output from the YDK conversion: (YAML) -&amp;gt;&amp;nbsp; JSON -&amp;gt; XML -&amp;gt; JSON -&amp;gt; (YAML)&lt;/P&gt;&lt;P&gt;Thanks in advance for any suggestion or input,&lt;/P&gt;&lt;P&gt;Luca&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2022 14:49:34 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4572688#M1525</guid>
      <dc:creator>ldacol</dc:creator>
      <dc:date>2022-03-17T14:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: Converting from XML to JSON (YAML) via YDK</title>
      <link>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4572751#M1526</link>
      <description>&lt;P&gt;Hi Luca&lt;/P&gt;
&lt;P&gt;I do not understand, why do you need convert XML to a dictionary, especially since the YDK does not support conversion from/to a dictionary?&lt;/P&gt;
&lt;PRE&gt;    with open(xml_file, "rb") as f:
        config_xml_yang = xmltodict.parse(f, xml_attribs=True)
&lt;/PRE&gt;
&lt;P&gt;You need simply read XML file to a string and give it to the codec:&lt;/P&gt;
&lt;PRE&gt;with open(xml_file, "rb") as f:
    config_xml = f(read)&lt;BR /&gt;decoded_entity = codec.decode(XML_PROVIDER, config_xml)&lt;BR /&gt;config_json = codec.encode(JSON_PROVIDER, decoded_entity)&lt;/PRE&gt;
&lt;P&gt;Yan Gorelik&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2022 15:43:40 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4572751#M1526</guid>
      <dc:creator>ygorelik</dc:creator>
      <dc:date>2022-03-17T15:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: Converting from XML to JSON (YAML) via YDK</title>
      <link>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4572764#M1527</link>
      <description>&lt;P&gt;Thank you for detailed informations. This code below was solved my problem too&lt;A href="https://serviceproviderslist.com/what-is-tailgating-in-cyber-security/" target="_self"&gt;.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;with open(xml_file, "rb") as f:
    config_xml = f(read)&lt;BR /&gt;decoded_entity = codec.decode(XML_PROVIDER, config_xml)&lt;BR /&gt;config_json = codec.encode(JSON_PROVIDER, decoded_entity)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2022 16:01:00 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4572764#M1527</guid>
      <dc:creator>ozdenkarabulut</dc:creator>
      <dc:date>2022-03-17T16:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: Converting from XML to JSON (YAML) via YDK</title>
      <link>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4573280#M1528</link>
      <description>&lt;P&gt;Hi Yan,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you, this worked for the openconfig models. I am getting "ydk.errors.YModelError: No YDK bundle installed for node path '&lt;A href="http://yang.juniper.net/junos/conf/interfaces:interfaces" target="_blank"&gt;http://yang.juniper.net/junos/conf/interfaces:interfaces&lt;/A&gt;'" when decoding a simple interfaces section Juniper specific. I have generated the bundle via ydg-gen&amp;nbsp; command "./generate.py --python --bundle profiles/bundles/juniper-junos_18_4_R3.json" which included all the modules under 18.4R4 and installed (see my previous notes - bundle name ydk-models-junos-18-4==18.4.post3). Is there any specific way to refer to it?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Luca&lt;/P&gt;&lt;PRE&gt;{
  "name": "junos-18_4",
  "version": "18.4R3",
  "core_version": "0.8.4",
  "author": "",
  "copyright": "",
  "description": "JunOS 18.4 Yang Model",
  "long_description": "JunOS 18.4 Yang Model",
  "models": {
    "description": "JunOS 18.4 Yang Model",
    "git": [
      {
        "url": "https://github.com/Juniper/yang.git",
        "commits": [
          {
            "dir": [
              "18.4/18.4R3/common",
              "18.4/18.4R3/junos/conf",
              "18.4/18.4R3/junos/conf-with-extensions"
            ]
          }
        ]
      }
    ]
  }
}&lt;/PRE&gt;&lt;PRE&gt;&amp;lt;interfaces xmlns="http://yang.juniper.net/junos/conf/interfaces"&amp;gt;
&amp;lt;interface&amp;gt;
    &amp;lt;name&amp;gt;xe-2/0/0&amp;lt;/name&amp;gt;
    &amp;lt;description&amp;gt;Test&amp;lt;/description&amp;gt;
    &amp;lt;unit&amp;gt;
    &amp;lt;name&amp;gt;0&amp;lt;/name&amp;gt;
    &amp;lt;family&amp;gt;
        &amp;lt;inet&amp;gt;
        &amp;lt;address&amp;gt;
            &amp;lt;name&amp;gt;1.1.1.1/30&amp;lt;/name&amp;gt;
        &amp;lt;/address&amp;gt;
        &amp;lt;/inet&amp;gt;
    &amp;lt;/family&amp;gt;
    &amp;lt;/unit&amp;gt;
&amp;lt;/interface&amp;gt;
&amp;lt;/interfaces&amp;gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2022 01:36:49 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4573280#M1528</guid>
      <dc:creator>ldacol</dc:creator>
      <dc:date>2022-03-18T01:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: Converting from XML to JSON (YAML) via YDK</title>
      <link>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4573706#M1529</link>
      <description>&lt;P&gt;Hi Luka&lt;/P&gt;&lt;P&gt;I am happy to hear that it worked for your purposes. To use the Juniper bundle in import statements I suggest you to run command 'pip list | grep ydk' and check how the bundle package is referred to. What is related to the codec operations, you do not need to have a model referral although model bundle installation is needed. The codec is smart enough to search for the bundle in the YDK installation directories. You just have to include the top level container/list into your XML/JSON string.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2022 15:25:20 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/converting-from-xml-to-json-yaml-via-ydk/m-p/4573706#M1529</guid>
      <dc:creator>yangorelik</dc:creator>
      <dc:date>2022-03-18T15:25:20Z</dc:date>
    </item>
  </channel>
</rss>

