Design for linux app to transform stdc out to json format

I wanted to grab the output of “ps aux” and format into json. There are examples of scripts to do this in languages I do not know. Also the comment was made that it would be hard to create a tool that could be configured to work with most output. But it didn’t stop me thinking it would be an interesting tool to write. It would require processing data comping in via the pipe operator. Something I had not done before. Turns out easy to do.

Anyway, I thought I would go down this rabbit hole and use it as a vehicle to practice my Linux coding skills and improve my c++17 / c++20 knowledge.

So here is my napkin scribbled requirements.

  • Input from either a file or the pipe operator.
  • Follow pipe best practices (need to find out what they are).

Command line options:

  • Read a file instead of the pipe.
  • Say if all the output is a single json document or each line of data is a json doc in it’s own right. This is useful if the pipe we’re reading is never ending. You could pipe the output to another tool that posts it to the web.
  • State which columns of the first row are used for object names.
  • State how many columns and their names for when first row of data is not descriptive. Like “ls -lha”.
  • Define which columns are parent objects containing the following column data as members.
  • Skip n lines of the start of the data.
  • How many lines, after the start to process.
  • Skip lines that do not have enough columns or has too many.
  • Ignore excessive columns. Default would be to include the rest of the line in the last object.
  • Set delimiter for columns, default to space.

So, for example, “ps aux”
Lets say the output is the following.

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 164780 11016 ? Ss Mar06 0:12 /sbin/init splash
root 2 0.0 0.0 0 0 ? S Mar06 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< Mar06 0:00 [rcu_gp]
fred 6 0.0 0.0 0 0 ? I< Mar06 0:00 [kworker/0:0H-events_highpri]
tom 12 0.0 0.0 0 0 ? S Mar06 0:22 [ksoftirqd/0]
tom 13 0.2 0.0 0 0 ? I Mar06 32:49 [rcu_sched]

ps aux | tojson -p -c USER,PID -w 4 -s
We could format it to get

{
    "root":
    {
        "1":
       {
              "%CPU":0.0,
              "%MEM":164780
       },
        "2":
       {
              "%CPU":0.0,
              "%MEM":0
       },
        "3":
       {
              "%CPU":0.0,
              "%MEM":0
       }
    },
    "fred":
    {
        "6":
       {
              "%CPU":0.0,
              "%MEM":0
       }
    },
    "tom":
    {
        "12":
       {
              "%CPU":0.0,
              "%MEM":164780
       },
        "13":
       {
              "%CPU":0.2,
              "%MEM":0
       }
    }
}

What do you think? Any additions?

Ta,
Richard.

1 Like

Looks like that could work as long as you are also handling escape characters properly.

Why not modify ps to allow JSON output?

1 Like

^Asking the real questions.

Granted, I think PS is written in C, but this would be a good attempt to convert to C++ or write a C with Objects version of it and thing make that JSON output a command line feature.