Random texts

jq

I found myself having to merge two json arrays of objects based on an ID in a shell script. #jq as a tool was pretty much a given and a quick search resulted in several solutions. None of them worked for me, as apparently IDs usually are not numbers anymore and everyone expects them to be strings. My IDs were numbers and jq blamed me for it: jq: error (at <unknown>): Cannot use number (4584) as object key Here is my solution, which is basically copy&paste from JSON joins with minor additions to convert the field into a string.

jq -n --slurpfile file1 groupsize --slurpfile file2 grouplist '
# leftJoin(a1; a2; field) expects a1 and a2 to be arrays of JSON objects
# and that for each of the objects, the field value is a string.
# A left join is performed on "field".
def leftJoin(a1; a2; field):
# hash phase:
(reduce a2[] as $o ({}; . + { ($o | field | tostring): $o } )) as $h2
# join phase:
| reduce a1[] as $o ([]; . + [$h2[$o | field | tostring] + $o ])|.[];

leftJoin( $file2; $file1; .id )
'