From b26601726d3bc79c04b58afc1ab848a9d8c9d6e0 Mon Sep 17 00:00:00 2001
From: Recolic K <bensl@microsoft.com>
Date: Tue, 7 Dec 2021 16:27:14 +0800
Subject: [PATCH] change single-value output, ignore empty header row

---
 json2table.cc | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/json2table.cc b/json2table.cc
index 7f794f4..14d2151 100644
--- a/json2table.cc
+++ b/json2table.cc
@@ -76,12 +76,11 @@ void naive_json_access_path(json &input, rlib::string json_path) {
 int main(int argc, char **argv) {
     rlib::opt_parser args(argc, argv);
     if(args.getBoolArg("-h", "--help")) {
-        rlib::println("json2table version 1.0.4, maintainer Recolic Keghart <root@recolic.net>");
+        rlib::println("json2table version 1.0.5, maintainer Recolic Keghart <root@recolic.net>");
         rlib::println("Usage: cat xxx.json | json2table");
         rlib::println("Usage: curl https://myapi/getJson | json2table /path/to/subobject");
         rlib::println("Set --programming / -p to make the output easier for program to process. ");
         rlib::println("You can use /path/to/col1,col2,col8 to select multiple columns. ");
-        rlib::println("This tool has a stable CLI interface between different version, unless explicitly warned.  ");
         return 1;
     }
     program_mode = args.getBoolArg("-p", "--programming");
@@ -93,7 +92,7 @@ int main(int argc, char **argv) {
 
     //////////////////////////////////////////////////////////////////////
 
-    vector<string> headers;
+    vector<string> headers; // it can be empty
     vector<vector<string>> rows(1);
     size_t curr_row_pos = 0;
     json_decay_single_element_array(input);
@@ -135,9 +134,8 @@ int main(int argc, char **argv) {
         }
     }
     else {
-        // No way to create table. Just print and go.
-        rlib::println("Single value:", json_to_string(input));
-        return 0;
+        // No way to create table. It's a single value, just put it in a 1x1 table. 
+        rows[curr_row_pos].emplace_back("VAL: "s + json_to_string(input));
     }
 
     ////////////////////// Print-out the table
@@ -153,7 +151,7 @@ int main(int argc, char **argv) {
         fort::char_table table;
         table << fort::header;
         for(auto &ele : headers) table << ele;
-        table << fort::endr;
+        if(!headers.empty()) table << fort::endr;
         for(auto &row : rows) {
             for(auto &ele : row) table << ele;
             table << fort::endr;
-- 
GitLab