QLands
QLands

Reputation: 2586

jq: join array elements from different files into one array

I have a series of JSON files like these:

[
  {
    "bent_general": "0",
    "bext_general": "0",
    "date_yyyymmdd": "20171104",
    "end_time": "2017-11-07T02:58:56",
    "householdid": "EAM2231"
  },
  {
    "bent_general": "2",
    "bext_general": "8",
    "date_yyyymmdd": "20171126",
    "end_time": "2017-12-07T05:58:56",
    "householdid": "EAM1234",
  }
]

I need to combine them in one big file with the same structure:

[
  {
    "data_from": "first_file"
  },
  {
    "data_from": "second_file"
  }
]

I tried: jq -s . file1 file2

But the result is:

[
  [
    {
      "data_from": "first_file"
    }
  ]
  ,
  [
    {
      "data_from": "second_file"
    }
  ]
]

Any idea is appreciated.

Upvotes: 0

Views: 1051

Answers (3)

Artem Trunov
Artem Trunov

Reputation: 1415

I was able to solve this with:

jq -s 'flatten' file1 file2 ...

So -s (slurp) assembles an array of files' contents (arrays), and flatten removes the extra [] levels and the output is an array of all records from all files.

Upvotes: 1

KevinS
KevinS

Reputation: 8647

I had the same issue and found my solution on github: https://github.com/stedolan/jq/issues/805#issuecomment-109783967

Using the -s option will return an array containing the contents of the input JSON files, which, in your case, are also arrays. This means that you need to spread (…[]) two array levels, and collect ([…]) the result.

jq -s '[.[][]]' a-*.json > manifest.json

Upvotes: 2

peak
peak

Reputation: 116870

It's not entirely clear from the question what the expected output is supposed to be, but it looks like you want to concatenate the arrays.

If that's the case and if your file list is file1 file2 ... then the following invocation would be appropriate:

jq add file1 file2 ...

p.s. In future, please follow the guidelines at http://stackoverflow.com/help/mcve minimal complete verifiable example

Upvotes: 2

Related Questions