Home > Enterprise >  extract value from subarray and combine with upper value using jq to csv
extract value from subarray and combine with upper value using jq to csv

Time:09-30

I have a json file with a sub-array and I want to get each sub-array with the same id in separate lines. json:

{
    "success": true,
    "status": {
        "http": {
            "code": 200,
            "message": "OK"
        }
    },
    "result": [{
        "id": "123456789",
        "start_date": "2021-01-01 08:17:39.989",
        "snippets": [{
            "transcript": "yes",
            "matched_entry": null,
            "start": "2021-01-16 11:32:25.922"
        }, {
            "transcript": null,
            "matched_entry": null,
            "start": "2021-01-16 11:32:38.179"
        }]
    }, {
        "id": "987654321",
        "start_date": "2021-01-01 08:17:39.989",
        "duration_total": 301,
        "snippets": [{
            "transcript": "yes",
            "matched_entry": null,
            "start": "2021-01-01 08:17:54.055"
        }, {
            "transcript": "something",
            "matched_entry": " meta entry",
            "start": "2021-01-01 08:18:11.028"
        }, {
            "transcript": "no",
            "matched_entry": null,
            "start": "2021-01-01 08:18:24.057"
        }]
    }]
}

I try to get:

123456789, yes , null, "2021-01-16 11:32:25.922"
123456789, null, null, "2021-01-16 11:32:38.179"
987654321, yes, null, "2021-01-01 08:17:54.055"
987654321, something, "meta entry", "2021-01-01 08:18:11.028"
987654321, no, null, "2021-01-01 08:18:24.057"

first attempt was:

jq -rc ".result[] | {id: .id, snippetsTranscript: .snippets[].transcript, snippetsMatchedEntry: .snippets[].matched_entry, snippetsStart: .snippets[].start}" 210101_210121_copy.json

but the result is that every combination was returned:

{"id":"123456789","snippetsTranscript":"yes","snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:25.922"}
{"id":"123456789","snippetsTranscript":"yes","snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:38.179"}
{"id":"123456789","snippetsTranscript":"yes","snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:25.922"}
{"id":"123456789","snippetsTranscript":"yes","snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:38.179"}
{"id":"123456789","snippetsTranscript":null,"snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:25.922"}
{"id":"123456789","snippetsTranscript":null,"snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:38.179"}
{"id":"123456789","snippetsTranscript":null,"snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:25.922"}
{"id":"123456789","snippetsTranscript":null,"snippetsMatchedEntry":null,"snippetsStart":"2021-01-16 11:32:38.179"} ...

the second attempt was:

jq -rc ".result[] | {id: .id, snippetsMatchedEntry: [.snippets[].matched_entry], snippetsStart: [.snippets[].start], snippetsTranscript: [.snippets[].transcript]}" 210901_210921_copy.json

but the result is:

{"id":"123456789","snippetsMatchedEntry":[null,null],"snippetsStart":["2021-01-16 11:32:25.922","2021-01-16 11:32:38.179"],"snippetsTranscript":["yes",null]}
{"id":"987654321","snippetsMatchedEntry":[null," meta entry",null],"snippetsStart":["2021-01-01 08:17:54.055","2021-01-01 08:18:11.028","2021-01-01 08:18:24.057"],"snippetsTranscript":["yes","something","no"]}

Is this possible with jq?

CodePudding user response:

For each element of the result array, create an object with just the id field, and for each element of the snippets sub-array, add it:

.result[] | {id}   .snippets[]

If you don't need all fields of the snippets array, simply declare them as before

.result[] | {id}   (.snippets[] | {transcipt, matched_entry, start})

Try it on jqplay.org

  • Related