Skip to content

[sonic-db-cli] Support --json option#1205

Open
bgallagher-nexthop wants to merge 2 commits into
sonic-net:masterfrom
nexthop-ai:sonic-db-cli-json
Open

[sonic-db-cli] Support --json option#1205
bgallagher-nexthop wants to merge 2 commits into
sonic-net:masterfrom
nexthop-ai:sonic-db-cli-json

Conversation

@bgallagher-nexthop

@bgallagher-nexthop bgallagher-nexthop commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Fixes #1204

Background

$> sonic-db-cli STATE_DB HGETALL "TRANSCEIVER_VDM_REAL_VALUE|Ethernet16" | jq
jq: parse error: Invalid numeric literal at line 1, column 28

sonic-db-cli does not output valid JSON, because it is designed to output the Python repr() representation of a dictionary. This means it does not work with tools like jq to format the output in a more readable manner.

Changes

This PR addresses that problem by introducing a new CLI option to sonic-db-cli: --json or -j. This causes sonic-db-cli to output valid JSON that is usable with tools like jq without breaking any backwards compatibility with users or tooling that relies on sonic-db-cli's current output format.

Testing

New unit-tests have been written.

Tested manually on a network switch:

~$ sonic-db-cli --json STATE_DB HGETALL "TRANSCEIVER_DOM_REAL_VALUE|Ethernet440" | jq
{}
~$ sonic-db-cli --json STATE_DB HGETALL "TRANSCEIVER_INFO|Ethernet440" | jq
{
  "media_interface_technology": "Copper cable unequalized",
  "manufacturer": "Amphenol",
  "ext_rateselect_compliance": "N/A",
  "cmis_rev": "5.0",
  "host_lane_count": "8",
....
}
~$ sonic-db-cli --json STATE_DB HGETALL "TRANSCEIVER_STATUS|Ethernet440" | jq
{
  "module_state": "ModuleReady",
  "module_fault_cause": "No Fault detected",
  "last_update_time": "Wed Jun 10 16:59:03 2026"
}
~$ sonic-db-cli --json STATE_DB KEYS "TRANSCEIVER_STATUS" | jq
[]
~$ sonic-db-cli --json STATE_DB KEYS "TRANSCEIVER_INFO*" | jq
[
  "TRANSCEIVER_INFO|Ethernet416",
  "TRANSCEIVER_INFO|Ethernet384",
  "TRANSCEIVER_INFO|Ethernet424",
  "TRANSCEIVER_INFO|Ethernet432",
  "TRANSCEIVER_INFO|Ethernet408",
  "TRANSCEIVER_INFO|Ethernet400",
  "TRANSCEIVER_INFO|Ethernet392",
  "TRANSCEIVER_INFO|Ethernet440"
]

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@bgallagher-nexthop

Copy link
Copy Markdown
Contributor Author

#1206 & sonic-net/sonic-buildimage#27836 are published to fix the build failure on this PR (it's a breakage on master).

@bgallagher-nexthop bgallagher-nexthop marked this pull request as ready for review June 11, 2026 21:59
@prgeor prgeor requested review from Copilot and qiluo-msft June 11, 2026 22:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an optional JSON output mode to sonic-db-cli so command results can be consumed by JSON tools (e.g., jq) without changing the existing default output format used by current tooling.

Changes:

  • Add -j/--json CLI flag and plumb it through command execution to select JSON formatting.
  • Implement swss::RedisReply::to_json_string() to serialize hiredis replies into valid JSON (with HGETALL / HSCAN field-maps as JSON objects).
  • Add unit tests for JSON output and update the CLI help golden output.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/cli_ut.cpp Adds coverage validating JSON formatting across several Redis reply types.
tests/cli_test_data/cli_help_output.txt Updates expected sonic-db-cli help text to include --json.
sonic-db-cli/sonic-db-cli.h Extends Options and executeCommands() signature to carry JSON mode.
sonic-db-cli/sonic-db-cli.cpp Implements -j/--json parsing and switches output formatting based on the flag.
common/redisreply.h Declares new RedisReply::to_json_string() API.
common/redisreply.cpp Implements JSON conversion logic using nlohmann JSON.
Comments suppressed due to low confidence (1)

sonic-db-cli/sonic-db-cli.cpp:238

  • --namespace is declared with optional_argument, so --namespace=asic0 will set optarg, but the current parsing ignores optarg and instead consumes the next argv token (db_or_op) as the namespace. This breaks a valid getopt_long invocation form.
                case 'j':
                    options.m_json = true;
                    break;

                case 'n':
                    if (optind < argc)
                    {
                        options.m_namespace = argv[optind];
                        optind++;
                    }
                    else
                    {
                        throw invalid_argument("namespace value is missing.");
                    }
                    break;

Comment thread tests/cli_ut.cpp
Comment thread sonic-db-cli/sonic-db-cli.cpp
Comment thread sonic-db-cli/sonic-db-cli.h
@mssonicbld

Copy link
Copy Markdown
Collaborator

Hi, there are workflow run(s) waiting for approval, you may be first-time contributor. I will notify maintainers to help approve once PR is approved. Thanks!

---Powered by SONiC BuildBot

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 12, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: bgallagher-nexthop / name: Brian Gallagher (02427a4, 2090c48)

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Brian Gallagher <bgallagher@nexthop.ai>
Signed-off-by: Brian Gallagher <bgallagher@nexthop.ai>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
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.

sonic-db-cli does not output valid JSON

3 participants