Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq: output array of json objects [duplicate]

Tags:

bash

jq

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

like image 877
Mauricio Trajano Avatar asked Jun 27 '16 19:06

Mauricio Trajano


1 Answers

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]"   } ] 
like image 88
chepner Avatar answered Oct 07 '22 10:10

chepner