2016-06-27 8 views
9

मैं इनपुट है कहोJQ:</p> <pre><code>{ "name": "John", "email": "[email protected]" } { "name": "Brad", "email": "[email protected]" } </code></pre> <p>मैं उत्पादन कैसे मिलता है: json के उत्पादन सरणी वस्तुओं

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

मैंने कोशिश की दोनों:

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

और

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

जो दोनों मुझे उत्पादन

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

मैं भी दस्तावेजों में एक सरणी उत्पादन के लिए कोई विकल्प नहीं देखा दे दी है, किसी भी मदद की सराहना की

+0

मैं अज्ञात सरणी होने के बजाय नई सरणी में नाम कैसे दे सकता हूं? तो {"लोग": [{"नाम": "ब्रैड", "ईमेल": "[email protected]"}]} – archcutbank

+0

@ user372429 आप अपने आउटपुट के आस-पास {people:} को लपेटेंगे, इसलिए इसे देखना चाहिए कुछ ऐसा: jq -s '{people:। } '

उत्तर

10

उपयोग 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

बिल्कुल सही, धन्यवाद! समय समाप्त होने पर स्वीकार करेंगे –

संबंधित मुद्दे