2016-06-27 26 views
9

Say Ho l'ingresso:jq: matrice di uscita di json oggetti

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

Come ottengo l'output:

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

Ho provato entrambi:

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

e

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

cui entrambi mi ha dato l'uscita

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

ho visto anche opzioni per un'uscita di matrice nelle documentazioni, qualsiasi aiuto apprezzato

+0

Come faccio a dare un nome al nuovo array invece di essere un array anonimo? Quindi {"persone": [{"nome": "Brad", "email": "[email protected]"}]} – archcutbank

+0

@ user372429 si dovrebbe semplicemente racchiudere {people:} attorno all'output, quindi dovrebbe apparire qualcosa come: jq -s '{people:. } '

risposta

10

Utilizzare la modalità slurp:

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]" 
    } 
] 
+0

Perfetto, grazie! Accetterà quando il tempo è scaduto –