2016-07-15 82 views
0

我想通過通過標籤[]選擇這JSON outputJQ過濾器包括unfound選擇

標記過濾,如果存在鍵顯示所選擇的值。

我的JQ過濾器看起來是這樣的:

jq -r ".Reservations[] 
| [.OwnerId, 
    .Instances[].InstanceId, 
    .Instances[].Placement.AvailabilityZone, 
    (.Instances[].Tags[]?|select(.Key==OtherTag)|.Value), 
    (.Instances[].Tags[]?|select(.Key==Name)|.Value) ] 
| @csv" 

輸出看起來像這樣

"xxxxxxxxxx9,i-d414ce0b,ap-southeast-2b,webserver2" 

我想它包括毫無根據選擇的標籤,並在CSV填充它作爲空場 象下面這樣:

"xxxxxxxxxx9,i-d414ce0b,ap-southeast-2b,,webserver2" 

我應該在JQ如何實現呢?有人能給我一個例子嗎?

+0

所以我在這裏做了一點進步的解決方案。通過使用地圖和選擇。 '。保留[] | [.OwnerId,.Instances []。InstanceId,.Instances []。Placement.AvailabilityZone,(.Instances []。Tags | map(if(select(.Key ==「Name」))then .Value else「」end )),(.Instances []。Tags | map(if(select(.Key ==「OtherTag」)).Value else「」end))]'但是我現在結果爲空數組[ 。無論如何將空數組轉換爲空值?基本上壓縮該數組的所有值到一個字符串?希望這是有道理的 – hutanrimba

回答

0

而不是使用select/1的,使用filter/1,其中:

def filter(f): if f then . else "" end; 
0

下面是使用from_entries處理.Tags

.Reservations[] 
| .OwnerId as $owner 
| .Instances[] 
| .InstanceId as $id  
| .Placement.AvailabilityZone as $az 
| .Tags 
| from_entries 
| "\($owner),\($id),\($az),\(.OtherTag//""),\(.Name//"")"