Home > OS >  JQ stop after the first match
JQ stop after the first match

Time:05-04

I have a large file (about 500 megabytes) and the data in it is in JSON format.

{
    "0001": [
        "aaaaa",
        "qqqqq"
    ],
    "0002": [
        "aaaaa"
    ],
    "0003": [
        "ccccc"
    ],
    "0004": [
        "bbbbb"
    ]
...
}

I need to extract from it:

aaaaa
qqqqq

At the moment and I do the following jq -r 'try . "0001" | .[]' ./1.txt, it works, but the problem is that it takes a very long time because the search continues on through the whole file, instead of stopping immediately after the first match.

Please advise me a way to stop further scouting if an input has already been found. I know that there is a first(inputs | ), but I don't understand how to implements this command.

CodePudding user response:

This works for the sample input:

jq --null-input --raw-output --stream 'label $out | inputs | if .[0][0] == "0001" then (if length == 2 then .[1] else break $out end) else empty end' file

CodePudding user response:

If it is known beforehand that the relevant key is the first one in the JSON object, then the solution using --stream and first/1 as given by @pmf is applicable; otherwise, it could be adapted as follows:

jq --stream -n 'first(fromstream(1 | truncate_stream(inputs|select(.[0][0] =="0001"))))[]' input.json
  • Related