Juicy
Juicy

Reputation: 12520

Merge arrays in object

I have an object that is just a bunch of arbitrary keys with each an array:

{
  "foo": [
    "hello",
    "world"
  ],
  "bar": [
    "foobar"
  ]
}

How can I return the merged arrays in this object. The expected output would be:

[
  "hello",
  "world",
  "foobar"
]

Upvotes: 1

Views: 120

Answers (2)

peak
peak

Reputation: 116740

Generalizing a bit:

jq '[..|scalars]' input.json

Upvotes: 1

Carter
Carter

Reputation: 439

Create a list of the values and concatenate the elements in that list:

[.[]] | add

Create a list of each element in each array:

[.[][]]

I'd prefer the first one since it parses easier in my mind.

Upvotes: 3

Related Questions