The trick of course if to create an array of JSON objects which are comma separated key, value pairs.
I don't think a tool like
jq is designed to "
think for you, as to how to create the array of JSON objects from raw data".
That's up to you to design, specific to this single application first.
Personally, I think you are jumping to tools like
jq too quickly before you fully understand / design your algorithm to take your raw output and create an array of JSON objects.
If it was me, I would write this in PHP and it would only take a few lines of PHP code to take your input and create the desired JSON file.
Either way, bash or PHP or Javascript, it a simply matter:
- Taking the output of the command as text as input into your code.
- Parsing this data.
- Creating a JSON object from each line of parsed data (the values), keeping the header line as the key to the JSON objects..
- Add the JSON object above to an another array.
- Repeat for the next line of data in your output
I have done this 100s of time (using PHP) and there is generally no short-cut to writing your algorithm to take raw text as data and parsing it into a JSON object and then creating an array of JSON objects.
However, generally speaking I create an array of arrays and then convert the final array to JSON; but it really depends on the format of the final JSON object and if it needs to be
stringified to be transmitted over the net.
Also, I would caution you not to try to create "one parser to rule them all" as the output of other commands will like need a different parser.
Then when you have done this for a number of different commands, you can consider if you want to build "the mother of all UNIX commands to JSON parser".
Reference:
PHP: json_encode - Manual