Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

Using JSON Compact in PyEZ

[ Edited ]
‎04-26-2020 04:27 AM

I have a preference to use the JSON Compact format over the default non-compact. However. PyEZ always throws up errors with the output stating the formating is incorrect. An example script below, which comes back with :

 

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 478 (char 477)

 

(The Junos device is a vMX running on my local machine configured with set system export-format state-data json compact)

 

It would seem the formatting coming back is not correct whilst it is JSON Compact style it has formatting errors and this can be seen if pasting this into JSON formatters. I wonder if therefore using JSON Compact with PyEZ is supported properly or if I should revert to the non-compact JSON which works out of the box. However, I have a personal preference for the JSON Compact as it's much simplier to work with in Python. I cannot find any mention of PyEz using the JSON Compact format so I wonder if this is the issue. Does anyone have any thoughts on the approach or support for JSON Compact with PyEZ?

 

import json
import sys
from jnpr.junos import Device
from jnpr.junos.exception import ConnectError
import pprint


from pprint import pprint
from jnpr.junos import Device

dev = Device(host="172.25.0.2", password="ot5jainupha5If7oosoo8pho", normalize=True)
dev.open()
output = dev.rpc.get_route_information({'format':'json'})
pprint(output)
dev.close()
3 REPLIES 3
Highlighted
Junos Automation (Scripting)

Re: Using JSON Compact in PyEZ

‎04-27-2020 04:56 AM

Hi

 

I believe json compact format is not supported for junos although the option for json compact is provided. It is a design limitation.

 

So you may have to use the non-compact format.

 

Hope this helps

Highlighted
Junos Automation (Scripting)

Re: Using JSON Compact in PyEZ

‎04-29-2020 06:27 AM

Thank you for taking the time to answer my question. This definitely seemed to be the case. The fact you can get structured data from JunOS is a major plus but the compact JSON would have been the icing on the cake. Right now I am using XML then XMLtoDICT in Python which seems to work quite well.

Highlighted
Junos Automation (Scripting)

Re: Using JSON Compact in PyEZ

‎04-29-2020 09:07 PM

Hi Gwoodwa1

 

I agree however to implement the compact jason it would require a lot of change in the code which may be done in later releases but for now it is not.

I am glad you have a workaround that.

 

Please mark the answer as accepted solutions if it helped you as it may benefit other users as well.