jq: How to filter an array and add suffixes to values

published Nov 11, 2015 03:36   by admin ( last modified Nov 11, 2015 03:36 )

Given the output of the jlist CLI command from pm2, you could filter it like this:

jq '.[]|{service:(.name| . += "memory"),  mem: .monit.memory},{ cpu: .monit.cpu, status: .pm2_env.status}'

Explanation

.[] means for each object in array, basically. The pipe afterwards will be executed once per array object

(.name| . += "memory") means pipe value of the name key to the next function, represent it there with a dot, and add the string "memory" to it