This sounds like a basic question but I have been searching for a better solution for some time already...
I have the following files:
file0.json:
{
"d": {
"aaData": [
{"a":1},
{"a":2},
{"a":3},
{"a":4}
]
}
}
and
file1.json:
[
{"b":1},
{"b":2},
{"b":7}
]
and I want a subtraction between then with the expected result:
{
"key": 3
}
{
"key": 4
}
I thought of the following command would do the job:
bash$ jq -s '[.d.aaData[].a] - [.[].b] | { key: .}' file0.json file1.json
but it gives me the following error:
Cannot index array with string "b"
What makes sense, since [.[1].b]
would't create an array, but something like [1][2][7]
the only way I managed to get the expected result was coding in my bash script the dirty solution:
bash$ a=$(jq '[.d.aaData[].a] | {key1: .}' file0.json)
bash$ b=$(jq '[.[].b] | {key2: . }' file1.json)
bash$ c=$(echo -n $a,$b)
bash$ echo $c
{ "key1": [ 1, 2, 3, 4 ] },{ "key2": [ 1, 2, 7 ] }
bash$ d=${c//"},{"/","}
bash$ echo $d
{ "key1": [ 1, 2, 3, 4 ] , "key2": [ 1, 2, 7 ] }
bash$ echo -n $d | jq '.key1 - .key2 | { key: .[] }'
{
"key": 3
}
{
"key": 4
}
I'm quite sure there is a better way to do it with jq.... just can't find how...
Select only the values in a
that are different from all values in b
.
jq -n --argfile a file0.json --argfile b file1.json '
$a.d.aaData[].a as $a
| select([$b[].b]
| all(. != $a))
| {key: $a}'
Here is a solution which uses the jq -
array difference operator:
[ $file0.d.aaData[] | {key:.a} ]
- [ $file1[] | {key:.b} ]
| .[]
Assuming filter.json
contains this filter then
jq -M -n --argfile file0 file0.json --argfile file1 file1.json -f filter.json
produces
{
"key": 3
}
{
"key": 4
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.