Map Junos OS Command Output to JSON in the CLI
Junos OS and Junos OS Evolved natively support XML for the operation and configuration of Junos devices. The CLI and the infrastructure communicate using XML. When you issue an operational command or display the configuration in the CLI, the CLI converts the output from XML into a readable text format for display.
Junos devices also support a JavaScript Object Notation (JSON) representation of the
operational command output and the configuration hierarchy. To display the command
output or configuration in JSON instead of in the default formatted ASCII text,
append the | display json option to the command in the CLI.
The following example executes the show chassis hardware command
and displays the output in JSON format. The response is identical to the NETCONF or
Junos XML protocol server response for the <get-chassis-inventory
format="json"> RPC request.
user@host> show chassis hardware | display json
{
"chassis-inventory" : [
{
"attributes" : {"xmlns" : "http://xml.juniper.net/junos/24.2R1/junos-chassis"},
"chassis" : [
{
"attributes" : {"junos:style" : "inventory"},
"name" : [
{
"data" : "Chassis"
}
],
"serial-number" : [
{
"data" : "XX00X00XXXXX"
}
],
"description" : [
{
"data" : "MX960"
}
],
"chassis-module" : [
{
"name" : [
{
"data" : "Midplane"
}
],
"version" : [
{
"data" : "REV 03"
}
],
"part-number" : [
{
"data" : "710-013698"
}
],
"serial-number" : [
{
"data" : "XX0001"
}
],
"description" : [
{
"data" : "MX960 Backplane"
}
],
"model-number" : [
{
"data" : "CHAS-BP-MX960-S"
}
]
},
//* additional JSON objects *//
]
}
]
}
]
}
You can configure how a Junos device emits configuration data in JSON format. To
configure the default export format, include the appropriate statement at the
[edit system export-format json] hierarchy level. You can
configure the JSON export format as one of the following:
-
ietf—Emit JSON configuration data as defined in IETF RFC 7951, JSON Encoding of Data Modeled with YANG. -
verbose—Emit all configuration objects as JSON arrays.
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 state
data 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 in
supported releases, configure the compact statement at the
[edit system export-format state-data json] hierarchy
level.
[edit] user@host# set system export-format state-data json compact
The following example executes the show system uptime command and
displays the output in both non-compact and compact JSON format.
user@host> show system uptime | display json
The following output shows the 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"
}
]
}
]
}
]
}
The output for the same command in compact JSON format is:
{
"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.