Search code examples
jsoneditjqstring-concatenation

Merge json array elements with jq


I want to merge array values using jq. In my input json there's an array times of nested arrays, each having (always) two string elements. I want those two string elements concatenated and the nested array removed so that there's ony one array left:

My input:

{
   "times":[
      [
         "7:29", "IN"
      ],
      [
         "10:29", "OUT"
      ]
   ],
   "foo":"bar"
}

My desired output is:

{
   "times":
   [
         "7:29 IN", "10:29 OUT"
   ],
   "foo":"bar"
}

This is how I merged the array elements, what's missing is to make a json array from it again:

jq    '.times | to_entries | .[] | (.value[0]+ " " + .value[1])'

Solution

  • jq '.times |= map(join(" "))' file
    

    yields:

    {
      "times": [
        "7:29 IN",
        "10:29 OUT"
      ],
      "foo": "bar"
    }