jsonjqelementwise-operations

Can jq add objects in two JSON object lists sequentially?


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.


Solution

  • One way would be using --slurpfile to read in the files as arrays, then use transpose and add to "zip" them:

    jq --slurpfile s1 file1.json --slurpfile s2 file2.json -n \
      '[$s1, $s2] | transpose[] | add'
    
    {
      "k11": "v111",
      "k12": "v112",
      "k21": "v211",
      "k22": "v212"
    }
    {
      "k11": "v121",
      "k12": "v122",
      "k21": "v221",
      "k22": "v222"
    }
    {
      "k11": "v131",
      "k12": "v132",
      "k21": "v231",
      "k22": "v232"
    }
    

    the two object lists (which can easily be turned into arrays)

    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:

    jq -s 'transpose[] | add' arrayfile1.json arrayfile2.json