I've tried using
jq "reduce inputs.skins as $s (.; .skins += $s)" file1.json file2.json > combined.json
but it just creates two boots.name and fun.name from each file
any way I can use jq and combine the objects and arrays without having duplicates?
I apologize for any confusion, jq is kind of complicated to find an easy tutorial for me to understand
file1.json
{
"skins": [
{
"Item Shortname": "boots.name",
"skins": [
2,
25,
41,
]
},
{
"Item Shortname": "fun.name",
"skins": [
12,
8,
]
}
]
}
file2.json
{
"skins": [
{
"Item Shortname": "boots.name",
"skins": [
2,
20,
]
},
{
"Item Shortname": "fun.name",
"skins": [
90,
6,
82,
]
}
]
}
combined.json
{
"skins": [
{
"Item Shortname": "boots.name",
"skins": [
2,
20,
25,
41,
]
},
{
"Item Shortname": "fun.name",
"skins": [
90,
6,
82,
12,
8,
]
}
]
}
The tricky part here is meeting the apparent uniqueness requirements, for which the following generic filter can be used:
This ensures the ordering is preserved. It's a bit tricky because it is completely generic -- it allows both
1
and"1"
and distinguishes between them, just asunique
does.(If the ordering did not matter, then you could use
unique
.)So, assuming an invocation along the lines of
you would place the above def followed by the following “main” program in program.jq:
A better solution would avoid the -s option (e.g. as shown below), but the above method of feeding the two files to jq is at least straightforward, and will work regardless of which version of jq you are using.
Solution using
input
One way to avoid slurping the two files would be to use
input
in conjunction with the -n command line option instead of -s. The "main" part of the jq program would then be as follows: