I have the following working jq transform. Input file (input.jsonl):
{"key": "key1", "value": {"one": 1, "two": 2}}
{"key": "key2", "value": {"three": 3, "four": 4}}
jq transform:
$ jq --compact-output '.key as $key|.value|to_entries|map({key: ($key), member:.key, score:(.value|tostring)})|.[]' input.jsonl
which correctly produces the desired output:
{"key":"key1","member":"one","score":"1"}
{"key":"key1","member":"two","score":"2"}
{"key":"key2","member":"three","score":"3"}
{"key":"key2","member":"four","score":"4"}
The input json is quite large - imagine thousands of entries in the "values" field of above example. I wish to perform this exact transformation in jq stream mode with the goal of avoiding memory pressure.
I have tried using jq foreach
to no avail. I cannot find a way to store the "key1" value to be referenced as entries in "values" are processed.
Example, using the same input as the working example:
$ jq -c --stream 'foreach . as $input ({};{in: $input};.)' input.jsonl
{"in":[["key"],"key1"]}
{"in":[["value","one"],1]}
{"in":[["value","two"],2]}
{"in":[["value","two"]]}
{"in":[["value"]]}
{"in":[["key"],"key2"]}
{"in":[["value","three"],3]}
{"in":[["value","four"],4]}
{"in":[["value","four"]]}
{"in":[["value"]]}
I need to reference the value "key1" when processing lines 2 and 3 above and so on for the remaining keys.
To reiterate, I desire the exact output from the non-stream version.