Fix invalid JSON output in occtl commands
Summary
This MR fixes two critical JSON formatting bugs in occtl that prevent proper parsing by standard JSON parsers:
Issue #661 (closed) - occtl iroutes outputs invalid JSON
The occtl -j show iroutes command was generating malformed JSON with:
- Missing array wrapper (used
{instead of[) - Duplicate
"IP"key in each object - No commas between objects
Fixed by:
- Using
print_array_block()/print_end_array_block()for proper array output - Adding
print_start_block()/print_end_block()for each user object - Removing the duplicate IP field
- Properly formatting commas between array elements
Issue #669 (closed) - Regression: Trailing commas in show sessions
The occtl -j show sessions command was generating invalid JSON with trailing commas after the "in_use" field, breaking strict JSON parsers.
Fixed by:
- Changed the last parameter of
print_single_value_int()for the"in_use"field from1to0 - This prevents adding a comma after the last field in the JSON object
Impact
These bugs affected anyone using occtl -j for automation or monitoring:
- JSON parsers failed to parse the output
- Automation scripts and monitoring tools broke
- Issue #669 (closed) is a regression - it was fixed in v0.12.5 but returned in v1.3.0
Testing
Tested on ocserv 1.3.0 with active VPN sessions. After the fix:
-
occtl -j show iroutesnow outputs valid JSON array -
occtl -j show sessions all/validoutputs valid JSON without trailing commas - Output can be successfully parsed by
python3 -m json.tooland other standard parsers
Changes
Single clean commit with minimal changes:
-
+7 -5 in
src/occtl/unix.c - Fixed both
handle_list_iroutes_cmd()andsession_info_cmd()functions - Code passes clang-format validation
Closes #661 (closed) Closes #669 (closed)