Closed Fribb closed 3 years ago
All the objects without "anidb_id"
field are merged into one object. On reducing by the *
operator same fields are overwritten by the right hand side object.
Okay, that makes sense. Do you have a tip on how this should be corrected so that the elements without "anidb_id" are still available individually in the output?
@Fribb: would something like this work?
jq -s 'flatten | group_by(.anidb_id) | map(if .[0] | has("anidb_id") then reduce .[] as $x ({}; . * $x) else .[] end)' anime-lists-reduced.json anime-offline-database-reduced.json > anime-list-full.json
@Fribb: would something like this work?
jq -s 'flatten | group_by(.anidb_id) | map(if .[0] | has("anidb_id") then reduce .[] as $x ({}; . * $x) else .[] end)' anime-lists-reduced.json anime-offline-database-reduced.json > anime-list-full.json
I will try this tomorrow and will give some feedback on the result.
@Fribb: would something like this work?
jq -s 'flatten | group_by(.anidb_id) | map(if .[0] | has("anidb_id") then reduce .[] as $x ({}; . * $x) else .[] end)' anime-lists-reduced.json anime-offline-database-reduced.json > anime-list-full.json
this seems to have worked, thank you very much.
I have a weird behaviour that I can't wrap my head around because it behaves differently depending on the size of the JSON arrays.
I have two JSON Arrays each in their own file and my goal is to merge them both into an "output" file so that the array elements are merged when it contains a specific key/value pair.
How that looks like, here are the two files I am talking about:
and the JQ command I use currently is
jq -s 'flatten | group_by(.anidb_id) | map(reduce .[] as $x ({}; . * $x))' anime-offline-database-reduced.json anime-lists-reduced.json > anime-list-full.json
Unfortunately, some elements, for example,
"mal_id": 34777
or"mal_id": 9062
are missing from the output file anime-list-full.jsonHowever, when I reduce the array to a few elements to try the command out for a more manageable pack of data, the behaviour is different. For example, I shorten the anime-offline-database-reduced.json to
and the anime-lists-reduced.json to
When I run the command above I get exactly what I want, 2 elements with both having the key/value pair with "mal_id" while only one has a key/value pair with "anidb_id" as it should be.
This doesn't make any sense to me on why the JQ command seems to ignore or discard those elements with a lot of array elements while it doesn't with a couple of array elements.