Jq: Can I convert a stream to a list?

Created on 28 Nov 2014  路  2Comments  路  Source: stedolan/jq

Say I have a file obj1.json like this:

{
  "name": "chalk",
  "id": 1
}

And another file obj2.json like this:

{
  "name": "cheese",
  "id": 0
}

How do I collect the objects into a list like this?

[
    {
      "id": 1,
      "name": "chalk"
    },
    {
      "id": 0,
      "name": "cheese"
    }
]

I tried to use jq's array constructor on the objects.

cat obj1.json obj2.json |
jq "[.]"

But it produces one list _per_ object instead of one list _of_ objects.

[
  {
    "id": 1,
    "name": "chalk"
  }
]
[
  {
    "id": 0,
    "name": "cheese"
  }
]
support

Most helpful comment

cat ... | jq -s .

All 2 comments

cat ... | jq -s .

Ah, I missed that in the manual.

--slurp/-s:

Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kelchy picture kelchy  路  4Comments

geoffeg picture geoffeg  路  3Comments

rclod picture rclod  路  4Comments

rokka-n picture rokka-n  路  4Comments

tischwa picture tischwa  路  4Comments