Reputation: 1
I am trying to extract data from some JSON with JQ - I have already got it down to the last level of data that I need to extract from, but I am completely stumped as to how to proceed with how this part of the data is formatted.
An example would be:
{
"values": [
[
1483633677,
42
]
],
"columns": [
"time",
"count_value"
],
"name": "response_time_error"
}
I would want to extract just the value for a certain column (e.g. count_value
) and I can extract it by using [-1]
in this specific case, but I want to select the column by its name in case they change in the future.
Upvotes: 0
Views: 500
Reputation: 116967
If the specified column name does not exist in .columns, then Jeff's filter will fail with a rather obscure error message. It might therefore be preferable to check whether the column name is found. Here is an illustration of how to do so:
jq --arg col count_value '
(.columns | index($col)) as $ix
| if $ix then .values[][$ix] else empty end' input.json
If you want an informative error message to be printed, then replace empty
with something like:
error("specified column name, \($col), not found")
Upvotes: 1
Reputation: 134571
If you're only extracting a single value and the arrays will always correspond with eachother, you could find the index in the columns
array then use that index into the values
array.
It seems like values
is an array of rows with those values. Assuming you want to output the values of all rows with the selected column:
$ jq --arg col 'count_value' '.values[][.columns | index($col)]' input.json
Upvotes: 1