I have 2 json files: a_names.json and b_names.json.
File a:
{
"Names": ["a","b","c"]
}
File b:
{
"Names": ["b","c"]
}
What i tried to do was (say i have a loop with counter ascending from 0):
a_names.Names | contains([b_names.Names[.counter]])
And the output is an error: Cannot index string with string.
However, next code provides a proper boolean output:
a_names.Names | contains(["b"])
My colleague said it’s happening due to me trying to refer to array 2 while being inside of the array 1, but we can’t find a solution.
P.S. – Same happens with any other "search" function, such as "any","IN", etc…
2
Answers
Solved:
In order to work with 2 arrays at the same time you will need to initialize reference to both of them in separate pipes and only after that you can use functions.
In the question,
a_names
is bound to the contents of the first file, whereas in practice the contents would more probably be bound to$a_names
, so let’s assume you invoke jq as follows:In this case, a suitable program would be:
However, you may also wish to consider the following:
Numerous other variants are possible, but I’d avoid
contains
in this case as its semantics is quite complex.If the arrays are known to have distinct items in strictly increasing order, or if the overhead of sorting them can be justified, then you could use
subset/1
defined as: