View Only
last person joined: yesterday 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  pyez and export-format state-data json compact

    Posted 04-08-2024 13:20

    hi expert 

    I  have a question  when i using pyez 

    here is my code 

    from jnpr.junos import Device
    from pprint import pprint
    def get_ospf_neighbor_information(username, pwd, host ):
        dev = Device(host=host, user=username, password=pwd)
        rpc = dev.rpc.get_ospf_neighbor_information({'format':'json'})
        return rpc
    if __name__ == "__main__": 
        ospf_r1 = get_ospf_neighbor_information('lab', 'lab123', '' )

    the code is work but if i change json format by this cli 

    export-format state-data json compact    

    system will report this error 

    JSONDecodeError                           Traceback (most recent call last)
    File ~/anaconda3/lib/python3.11/site-packages/jnpr/junos/, in _Connection.execute(self, rpc_cmd, ignore_warning, **kvargs)
        877 try:
    --> 878     return json.loads(rpc_rsp_e.text, strict=False)
        879 except ValueError as ex:
        880     # when data is {}{.*} types
    File ~/anaconda3/lib/python3.11/json/, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
        358     kw['parse_constant'] = parse_constant
    --> 359 return cls(**kw).decode(s)
    File ~/anaconda3/lib/python3.11/json/, in JSONDecoder.decode(self, s, _w)
        333 """Return the Python representation of ``s`` (a ``str`` instance
        334 containing a JSON document).
        336 """
    --> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
        338 end = _w(s, end).end()
    File ~/anaconda3/lib/python3.11/json/, in JSONDecoder.raw_decode(self, s, idx)
        352 try:
    --> 353     obj, end = self.scan_once(s, idx)
        354 except StopIteration as err:
    JSONDecodeError: Expecting property name enclosed in double quotes: line 14 column 9 (char 355)
    During handling of the above exception, another exception occurred:
    JSONLoadError                             Traceback (most recent call last)
    Cell In[1], line 14
          9     return rpc
         12 if __name__ == "__main__": 
    ---> 14     ospf_r1 = get_ospf_neighbor_information('lab', 'lab123', '' )
         15     pprint(ospf_r1)
    Cell In[1], line 7, in get_ospf_neighbor_information(username, pwd, host)
          5 dev = Device(host=host, user=username, password=pwd)
    ----> 7 rpc = dev.rpc.get_ospf_neighbor_information({'format':'json'})
          8 dev.close()
          9 return rpc
    File ~/anaconda3/lib/python3.11/site-packages/jnpr/junos/, in _RpcMetaExec.__getattr__.<locals>._exec_rpc(*vargs, **kvargs)
        358             rpc.attrib[k] = v
        360 # now invoke the command against the
        361 # associated :junos: device and return
        362 # the results per :junos:execute()
    --> 363 return self._junos.execute(rpc, **dec_args)
    File ~/anaconda3/lib/python3.11/site-packages/jnpr/junos/, in normalizeDecorator.<locals>.wrapper(*args, **kwargs)
         74 else:
         75     try:
    ---> 76         return function(*args, **kwargs)
         77     except Exception:
         78         raise
    File ~/anaconda3/lib/python3.11/site-packages/jnpr/junos/, in timeoutDecorator.<locals>.wrapper(*args, **kwargs)
         29 else:
         30     try:
    ---> 31         return function(*args, **kwargs)
         32     except Exception:
         33         raise
    File ~/anaconda3/lib/python3.11/site-packages/jnpr/junos/, in _Connection.execute(self, rpc_cmd, ignore_warning, **kvargs)
        882             return json.loads(re.sub(r"\s?{\s?}\s?", "", rpc_rsp_e.text))
        883         else:
    --> 884             raise JSONLoadError(ex, rpc_rsp_e.text)
        885 else:
        886     warnings.warn(
        887         "Native JSON support is only from 14.2 onwards", RuntimeWarning
        888     )
    JSONLoadError: JSONLoadError(reason: Expecting property name enclosed in double quotes: line 14 column 9 (char 355), 
    The offending config appears to be: 
    12:             "activity-timer" : "36"
    13:         }, 
    14:         {
    15:             "neighbor-address" : "", 
    16:             "interface-name" : "ge-0/0/2.0", 

    I am not sure how to change that ,but old json format is so difficult to read 

  • 2.  RE: pyez and export-format state-data json compact

    Posted 04-15-2024 15:21

    From the error message displayed, it looks as though the data structure is not correct, since JSONLoad isn't accepting it.  However, from the code snippet shown I can't see where the error is in the returned data, when using export-format state-data json compact.

    Maybe the following commands will help get the full JSON response back and then take a look to see if the JSON response is actually valid JSON or not, I suspect that there is issue in how the data is being returned by Junos.

    This is just performing a NETCONF RPC via SSH with the -s netconf argument.


    ssh user@device -s netconf

    <rpc><get-ospf-neighbor-information format="json"/></rpc>


    Then look at the response and see if you can see if anything is incorrect in the reply.   If it is a Junos issue with the output then either you will have to use the standard JSON output, or perhaps XML instead... Else discuss with JTAC.

    ssh user@device -s netconf
    <!-- No zombies were killed during the creation of this user interface -->
    <!-- user root, class super-user -->
    <nc:hello xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
    <rpc><get-ospf-neighbor-information format="json"/></rpc>
    <nc:rpc-reply xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="">
    <nc:rpc-reply xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="">
    <!-- session end at 2024-04-15 12:14:38 PDT -->

    Andy Sharp