Sam G
Sam G

Reputation: 161

JQ Error for parsing multiple array objects in JSON to CSV jq: error (at <stdin>:734665): Cannot index array with string

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

Answers (1)

Bertrand Martel
Bertrand Martel

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

Related Questions