tzippy
tzippy

Reputation: 6638

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])'

Upvotes: 1

Views: 1358

Answers (1)

oguz ismail
oguz ismail

Reputation: 50750

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

yields:

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

Upvotes: 3

Related Questions