Reputation: 161
I have a JSON result from an ElasticSearch query that provides multiple objects in the JSON result.
{
"buckets": [{
"perf_SP_percentiles": {
"values": {
"80.0": 0,
"95.0": 0
}
},
"perf_QS_percentiles": {
"values": {
"80.0": 12309620299487,
"95.0": 12309620299487
}
},
"latest": {
"hits": {
"total": 3256,
"max_score": null,
"hits": [{
"_source": {
"is_external": true,
"time_ms": 1492110000000
},
"sort": [
1492110000
]
}]
}
}
}]
}
I wrote the following jq with help from others
jq -r '.buckets[].latest.hits.hits[]._source | [."is_external",."time_ms"] | @csv'
I need to add the perf_QS_Percentiles to the CSV but getting an error.
jq -r '.buckets[].latest.hits.hits[]._source | [."is_external",."time_ms"], .buckets[].perf_QS_percentiles.values | [."80.0",."95.0"] | @csv'
I am getting an error jq: error (at <stdin>:734665): Cannot index array with string
. may be I am missing something here. I am reading the JQ manual https://stedolan.github.io/jq/manual/#Basicfilters to see how to parse different JSON objects in the array, but asking here as someone may be able to point out more easily.
Upvotes: 1
Views: 917
Reputation: 45432
You can use (....) + (....)
to create the array before piping to @csv
:
jq -r '.buckets[] |
(.latest.hits.hits[]._source | [."is_external",."time_ms"]) +
(.perf_QS_percentiles.values | [."80.0",."95.0"]) | @csv'
Upvotes: 1