Skip to content

Conversation

@Bojun-Feng
Copy link

What I did

Fixed show ip route <protocol> json commands producing invalid JSON output with namespace prefix.

Fix sonic-net/sonic-buildimage#24398
Fix #3687

How I did it

The root cause is show/bgp_common.py have a special if clause for protocol filters (bgp, static, etc.), triggering namespace-prefix output path even when JSON is requested.

Current logic:

if found_other_parms:
    print("{}:".format(ns))   # Prints ': ' on single-ASIC (ns='')
    print(output)
    continue                  # Skips JSON processing

Solution:

if found_other_parms and not found_json:
    print("{}:".format(ns))
    print(output)
    continue

We simply skip namespace-prefix path when JSON output is requested.
This allows JSON requests to fall through to proper JSON processing later at line 413.

How to verify it

Test protocol-filtered route commands with JSON output:

show ip route bgp json
show ip route static json
show ip route connected json

Verify output starts with { and is valid JSON (parseable with jq or python -m json.tool).

Verify non-JSON commands still work:

show ip route bgp
show ip route

Previous command output (if the output of a command-line utility has changed)

admin@switch:~$ show ip route bgp json | head
: {
  "0.0.0.0/0":[
    {
      "prefix":"0.0.0.0/0",
      "prefixLen":0,
      "protocol":"bgp",
      "vrfId":0,
      "vrfName":"default",
      "selected":true,

New command output (if the output of a command-line utility has changed)

admin@switch:~$ show ip route bgp json | head
{
  "0.0.0.0/0":[
    {
      "prefix":"0.0.0.0/0",
      "prefixLen":0,
      "protocol":"bgp",
      "vrfId":0,
      "vrfName":"default",
      "selected":true,
      "destSelected":true,

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 31, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@Bojun-Feng Bojun-Feng force-pushed the correct-json-formatting branch from e8debf2 to 0467615 Compare October 31, 2025 18:12
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Commands like 'show ip route bgp json' were producing invalid JSON
with a namespace prefix (': '), breaking JSON parsers and automation.

The issue occurred because protocol filters (bgp, static, etc.) triggered
the namespace-prefix output path even when JSON was requested. On
single-ASIC systems where namespace is empty, this resulted in ': '
being printed before the JSON output.

Fixed by skipping the namespace-prefix path when JSON output is
requested, allowing proper JSON processing and formatting.

Signed-off-by: Bojun Feng <[email protected]>
@Bojun-Feng Bojun-Feng force-pushed the correct-json-formatting branch from 0467615 to 86d7fb6 Compare October 31, 2025 18:19
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Adds test_show_ip_route_bgp_json to verify protocol-filtered route
commands with JSON output work correctly.

Test validates:
- Command executes successfully
- Output is valid parseable JSON
- No namespace prefix bug (': ' at start)
- Output starts with '{'
- BGP routes present in filtered output

This test covers the bug fix in show/bgp_common.py where protocol
filters (bgp, static, etc.) with JSON output were producing invalid
JSON with namespace prefix on single-ASIC systems.

Signed-off-by: Bojun Feng <[email protected]>
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: show ip route <protocol> json produces invalid JSON output [bug] show ip route bgp json has redudant leading colon which is invalid json data

2 participants