jq & JSON : extract arrays

Natarajan Santhosh
1 min readAug 1, 2023

--

Given this json, extract cpu array, duration and functions array as csv

 {
"__criblEventType": "stats",
"__ctrlFields": [],
"__final": false,
"__cloneCount": 0,
"cpu": {
"user": 40.172,
"system": 4.178
},
"duration": 39.86279200017452,
"functions": [
{
"bytesIn": 1166481,
"bytesOut": 1177821,
"duration": 1.4648340120911598,
"eventsIn": 810,
"eventsOut": 810,
"func": "eval"
}
],
"__pipeStats": true
}

To extract the `cpu` array, `duration`, and `functions` array from the given JSON and convert them into CSV format, you can use the `jq` command in Bash. Here’s how you can do it:

Assuming the JSON is stored in a file named `data.json`, you can use the following Bash command:

echo "CPU_USER,CPU_SYSTEM,DURATIONO,FUNCTION,DURATION,EVENTSIN,EVENTSOUT,BYTESIN,BYTESOUT" > output.csv
jq -r '(.cpu | .user, .system), .duration, (.functions[] | .func, .duration, .eventsIn, .eventsOut, .bytesIn, .bytesOut)' stats.json| paste -sd ',' >> output.csv

Explanation:
- `jq ‘.cpu | .user, .system, .duration, (.functions[] | .func, .duration) | @csv’ data.json`: This `jq` expression first accesses the `cpu` object using `.cpu`, then selects the fields `user`, `system`, and `duration` from the `cpu` object. Next, it uses `.functions[]` to iterate over each element in the `functions` array and selects the fields `func` and `duration` from each function object.
- `> output.csv`: This redirects the output of the `jq` command to a file named `output.csv`, which will contain the CSV-formatted data.

After running the command, the `output.csv` file will contain the following CSV data:

40.172,4.178,39.86279200017452,"eval",1.4648340120911598

--

--

No responses yet