jq: output array of json objects

BashJq

Bash Problem Overview


Say I have the input:

{
    "name": "John",
    "email": "[email protected]"
}
{
    "name": "Brad",
    "email": "[email protected]"
}

How do I get the output:

[
    {
        "name": "John",
        "email": "[email protected]"
    },
    {
        "name": "Brad",
        "email": "[email protected]"
    }
]

I tried both:

jq '[. | {name, email}]'

and

jq '. | [{name, email}]'

which both gave me the output

[    {        "name": "John",        "email": "[email protected]"    }]
[    {        "name": "Brad",        "email": "[email protected]"    }]

I also saw no options for an array output in the documentations, any help appreciated

Bash Solutions


Solution 1 - Bash

Use slurp mode:

> o --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.

$ jq -s '.' < tmp.json
[
  {
    "name": "John",
    "email": "[email protected]"
  },
  {
    "name": "Brad",
    "email": "[email protected]"
  }
]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMauricio TrajanoView Question on Stackoverflow
Solution 1 - BashchepnerView Answer on Stackoverflow