Convert Yang models to JSON

Is there any tool, to convert yang models to json?
So far, I have been trying to work with ‘pyang -f jsonxsl -o test.xsl tailf-common.yang’ but end up getting bunch of errors.
If I try the same command for other yang models that import tailf-common, I get an error, stating that ‘JSONXSL plugin needs a valid module’.

Any help will be appreciated.

1 Like

You probably do not want to convert tailf-common.yang itself, but you might need to have it on the search path. So the command would be something like

pyang -f jsonxsl -o test.xsl --path=$CONFD_DIR/src/confd/yang test.yang
1 Like

Does that solve the problem? I guess it would only give xsl format. To further convert it to json don’t we need to do as below:
$xsltproc test.xsl ietf-interfaces.yang

Hm, no. It answers “how to use -f jsonxsl with pyang”, which may not have been the intended original question. The result of the command is a XSL template that can be used to convert XML payload to JSON payload.

There is nothing like a “canonical JSON format of a YANG model” (none that I’m aware of). What comes close is a JSON format that can be consumed by Swagger - this is briefly described in the ConfD user guide and in more detail in this application note.

But note that if you just want to talk to your confd in JSON, use its RESTCONF interface and request JSON payload; see more in the user guide.

Thanks Martin @mvf! There is certainly no off the shelf tool kit to convert yang model to json model so that it can be used through Restcobf but then using pyang -f yin ietf-interfaces.yang > ietf.xml and then using python xmltodict library the same can be converted to json. I tried this today. But found that the json payload so generated guves a key value pair for every node, even ‘grouping’ which is not an actual data node. Alternatively, open daylight yang parser libraries do allow yang to json conversion. But the latter is a little uphill task though a good progress done because of poor Java doc and online help.

Why don’t use use the '| display json" pipe command in CLI to get the output of some instance data that you can use? It’s the dat instance and not the schema that you are interested in right? In which case you may load come configuration and they either use the CLI command or confd_load to output the configuration in JSON.