I am seeking the simultaneous iteration of two lists.
Input object list:
First Input Object List
{
"k11": "v111",
"k12": "v112"
}
{
"k11": "v121",
"k12": "v122"
}
{
"k11": "v131",
"k12": "v132"
}
Second Input Object List
{
"k21": "v211",
"k22": "v212"
}
{
"k21": "v221",
"k22": "v222"
}
{
"k21": "v231",
"k22": "v232"
}
Wanted Output Object List
{
"k11": "v111",
"k12": "v112"
"k21": "v211",
"k22": "v212"
}
{
"k11": "v121",
"k12": "v122"
"k21": "v221",
"k22": "v222"
}
{
"k11": "v131",
"k12": "v132"
"k21": "v231",
"k22": "v232"
}
There are no matching keys between the two object lists (which can easily be turned into arrays).
Thank you for reading this question.
3
Answers
One way would be using
--slurpfile
to read in the files as arrays, then usetranspose
andadd
to "zip" them:If the two files were already arrays, you can move from using
--slurpfile
to a single--slurp
(or-s
), then reading in the files as regular input files instead, and using the same "zipping" technique:As illustrated by the accepted answer, one approach to the problem is to convert both streams to arrays,
but this imposes an unnecessary memory requirement, as illustrated by the following solution,
which requires that only one of the two streams be "slurped", and which
incidentally also does not incur the various costs of
transpose
.I know this is cheating, I just wanted to see if it would work: a shell pipeline