Specify the Output Format for Operational Information Requests in a Junos XML Protocol Session
In a Junos XML protocol session, to request information about a Junos device, a
client application emits an <rpc>
element that encloses a
Junos XML request tag element. To request that the Junos XML protocol server return
the output in a specific format, the client application includes the optional
format
attribute in the opening operational request tag. The
application can request output in formatted ASCII text, JavaScript Object Notation
(JSON), or XML-tagged format. The basic syntax is as follows:
<rpc> <operational-request format="(ascii | json | text | xml)"> <!-- tag elements for options --> </operational-request> </rpc>
XML Format
By default, the Junos XML protocol server returns operational information in XML
format. If the format
attribute is set to xml
, or
if the format
attribute is omitted, the server returns the response
in XML. The following example requests information for the ge-0/3/0 interface and
omits the format
attribute.
<rpc> <get-interface-information> <brief/> <interface-name>ge-0/3/0</interface-name> </get-interface-information> </rpc>
The Junos XML protocol server returns the information in XML, which is identical to
the output displayed in the CLI when you include the | display xml
option after the operational mode command.
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.4R1/junos"> <interface-information xmlns="http://xml.juniper.net/junos/11.4R1/junos-interface" junos:style="brief"> <physical-interface> <name>ge-0/3/0</name> <admin-status junos:format="Enabled">up</admin-status> <oper-status>down</oper-status> <link-level-type>Ethernet</link-level-type> <mtu>1514</mtu> <source-filtering>disabled</source-filtering> <speed>1000mbps</speed> <bpdu-error>none</bpdu-error> <l2pt-error>none</l2pt-error> <loopback>disabled</loopback> <if-flow-control>enabled</if-flow-control> <if-auto-negotiation>enabled</if-auto-negotiation> <if-remote-fault>online</if-remote-fault> <if-device-flags> <ifdf-present/> <ifdf-running/> <ifdf-down/> </if-device-flags> <if-config-flags> <iff-hardware-down/> <iff-snmp-traps/> <internal-flags>0x4000</internal-flags> </if-config-flags> <if-media-flags> <ifmf-none/> </if-media-flags> </physical-interface> </interface-information> </rpc-reply>
ASCII Format
To request that the Junos XML protocol server return operational information as
formatted ASCII text instead of tagging it with Junos XML tag elements, the client
application includes the format="text"
or
format="ascii"
attribute in the opening request tag.
<rpc> <get-interface-information format="(text | ascii)"> <brief/> <interface-name>ge-0/3/0</interface-name> </get-interface-information> </rpc>
When the client application includes the format="text"
or
format="ascii"
attribute in the request tag, the Junos XML
protocol server formats the reply as ASCII text and encloses it in an
<output>
tag element. The format="text"
and format="ascii"
attributes produce identical output.
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.4R1/junos"> <output> Physical interface: ge-0/3/0, Enabled, Physical link is Down Link-level type: Ethernet, MTU: 1514, Speed: 1000mbps, Loopback: Disabled, Source filtering: Disabled, Flow control: Enabled, Auto-negotiation: Enabled, Remote fault: Online Device flags : Present Running Down Interface flags: Hardware-Down SNMP-Traps Internal: 0x4000 Link flags : None </output> </rpc-reply>
The following example shows the equivalent operational mode command executed in the CLI:
user@host> show interfaces ge-0/3/0 brief Physical interface: ge-0/3/0, Enabled, Physical link is Down Link-level type: Ethernet, MTU: 1514, Speed: 1000mbps, Loopback: Disabled, Source filtering: Disabled, Flow control: Enabled, Auto-negotiation: Enabled, Remote fault: Online Device flags : Present Running Down Interface flags: Hardware-Down SNMP-Traps Internal: 0x4000 Link flags : None
The formatted ASCII text returned by the Junos XML protocol server is identical to the CLI output except in cases where the output includes disallowed characters such as '<' (less-than sign), '>' (greater-than sign), and '&' (ampersand). The Junos XML protocol server substitutes these characters with the equivalent predefined entity reference of '<', '>', and '&' respectively.
If the Junos XML API does not define a response tag element for the type of output
requested by a client application, the Junos XML protocol server returns the reply
as formatted ASCII text enclosed in an <output>
tag element
even if XML-tagged output is requested.
The content and formatting of data within an <output>
tag
element are subject to change, so client applications must not depend on
them.
JSON Format
A client application can request operational and configuration information in JSON
format. To request that the Junos XML protocol server return operational information
in JSON format, the client application includes the format="json"
attribute in the opening request tag.
<rpc> <get-interface-information format="json"> <brief/> <interface-name>cbp0</interface-name> </get-interface-information> </rpc>
When the client application includes the format="json"
attribute in
the request tag, the Junos XML protocol server formats the reply using JSON.
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/16.1R1/junos"> { "interface-information" : [ { "attributes" : {"xmlns" : "http://xml.juniper.net/junos/16.1R1/junos-interface", "junos:style" : "brief" }, "physical-interface" : [ { "name" : [ { "data" : "cbp0" } ], "admin-status" : [ { "data" : "up", "attributes" : {"junos:format" : "Enabled"} } ], "oper-status" : [ { "data" : "up" } ], "if-type" : [ { "data" : "Ethernet" } ], "link-level-type" : [ { "data" : "Ethernet" } ], "mtu" : [ { "data" : "1514" } ], "speed" : [ { "data" : "Unspecified" } ], "clocking" : [ { "data" : "Unspecified" } ], "if-device-flags" : [ { "ifdf-present" : [ { "data" : [null] } ], "ifdf-running" : [ { "data" : [null] } ] } ], "ifd-specific-config-flags" : [ { "internal-flags" : [ { "data" : "0x0" } ] } ], "if-config-flags" : [ { "iff-snmp-traps" : [ { "data" : [null] } ] } ] } ] } ] } </rpc-reply>
By default, Junos devices emit JSON-formatted state data in non-compact format, which
emits all objects as JSON arrays. In Junos OS Release 24.2 and earlier and Junos OS
Evolved Release 24.2 and earlier, Junos devices support emitting the device’s
operational state in compact JSON format, in which only objects that have multiple
values are emitted as JSON arrays. To configure the device to emit compact JSON
format, configure the compact
statement at the [edit system
export-format state-data json]
hierarchy level.
user@host# set system export-format state-data json compact
The following example executes the show system uptime | display json
command and displays the output in non-compact and compact JSON format.
Non-compact JSON format:
{ "system-uptime-information" : [ { "attributes" : {"xmlns" : "http://xml.juniper.net/junos/18.1R1/junos"}, "current-time" : [ { "date-time" : [ { "data" : "2018-05-15 13:43:46 PDT", "attributes" : {"junos:seconds" : "1526417026"} } ] } ], "time-source" : [ { "data" : " NTP CLOCK " } ], "system-booted-time" : [ { "date-time" : [ { "data" : "2018-05-15 10:57:02 PDT", "attributes" : {"junos:seconds" : "1526407022"} } ], "time-length" : [ { "data" : "02:46:44", "attributes" : {"junos:seconds" : "10004"} } ] } ], "protocols-started-time" : [ { "date-time" : [ { "data" : "2018-05-15 10:59:33 PDT", "attributes" : {"junos:seconds" : "1526407173"} } ], "time-length" : [ { "data" : "02:44:13", "attributes" : {"junos:seconds" : "9853"} } ] } ], "last-configured-time" : [ { "date-time" : [ { "data" : "2018-05-02 17:57:44 PDT", "attributes" : {"junos:seconds" : "1525309064"} } ], "time-length" : [ { "data" : "1w5d 19:46", "attributes" : {"junos:seconds" : "1107962"} } ], "user" : [ { "data" : "admin" } ] } ], "uptime-information" : [ { "date-time" : [ { "data" : "1:43PM", "attributes" : {"junos:seconds" : "1526417026"} } ], "up-time" : [ { "data" : "2:47", "attributes" : {"junos:seconds" : "10034"} } ], "active-user-count" : [ { "data" : "1", "attributes" : {"junos:format" : "1 user"} } ], "load-average-1" : [ { "data" : "0.49" } ], "load-average-5" : [ { "data" : "0.19" } ], "load-average-15" : [ { "data" : "0.10" } ] } ] } ] }
Compact JSON format:
{ "system-uptime-information" : { "current-time" : { "date-time" : "2018-05-15 13:49:56 PDT" }, "time-source" : " NTP CLOCK ", "system-booted-time" : { "date-time" : "2018-05-15 10:57:02 PDT", "time-length" : "02:52:54" }, "protocols-started-time" : { "date-time" : "2018-05-15 10:59:33 PDT", "time-length" : "02:50:23" }, "last-configured-time" : { "date-time" : "2018-05-15 13:49:40 PDT", "time-length" : "00:00:16", "user" : "admin" }, "uptime-information" : { "date-time" : "1:49PM", "up-time" : "2:53", "active-user-count" : "1", "load-average-1" : "0.00", "load-average-5" : "0.06", "load-average-15" : "0.06" } } }
Change History Table
Feature support is determined by the platform and release you are using. Use Feature Explorer to determine if a feature is supported on your platform.
compact
statement at the
[edit system export-format state-data json]
hierarchy
level.