2016-10-11 25 views
0

json構造をjson構造を単純なオブジェクトの配列に変換するために 'jq' json processorを使いたいと思います。jqで多次元配列を作成する

{"nsgs": [ 
    { 
     "comments": "text1", 
     "properties": { 
      "securityRules": [ 
       { 
        "name": "1", 
        "properties": { 
         "protocol": "TCP", 
         "sourcePortRange": "*" 
        } 
       }, 
       { 
        "name": "2", 
        "properties": { 
         "protocol": "UDP", 
         "sourcePortRange": "*" 
        } 
       } 
      ] 
     } 
    }, 
    { 
     "comments": "text2", 
     "properties": { 
      "securityRules": [ 
       { 
        "name": "3", 
        "properties": { 
         "protocol": "TCP", 
         "sourcePortRange": "*" 
        } 
       }, 
       { 
        "name": "4", 
        "properties": { 
         "protocol": "UDP", 
         "sourcePortRange": "*" 
        } 
       } 
      ] 
     } 
    } 
]} 

そして、何私が取得したいことは次のとおりです:

私の構造は、このようなものです

[ 
{ "comments": "text1", 
    "name": "1", 
    "protocol": "TCP", 
    "sourcePortRange": "*" 
}, 

{ "comments": "text1", 
    "name": "2", 
    "protocol": "UDP", 
    "sourcePortRange": "*" 
}, 

{ "comments": "text2", 
    "name": "3", 
    "protocol": "TCP", 
    "sourcePortRange": "*" 
}, 

{ "comments": "text2", 
    "name": "4", 
    "protocol": "UDP", 
    "sourcePortRange": "*" 
} 
] 

私はアプローチの多くを試みたが、何も助けません。

助けていただければ幸いです。

答えて

0

要求されるように読みやすいここにレイアウトされ、次のフィルタは、入力を集約します:

.nsgs 
| map(.comments as $comments 
     | .properties.securityRules[] 
     | {comments: $comments, 
     name, 
     protocol: .properties.protocol, 
     sourcePortRange: .properties.sourcePortRange }) 

あなたは最後の2行で繰り返しを避けたかった場合は、あなたが最後の4行を置き換えることができます:ここでは

 | {comments: $comments, name } 
     + (.properties | {protocol, sourcePortRange})) 
+0

は、優れたソリューションをありがとう! –

1

は別のソリューションです:

.nsgs | map({comments} + (.properties.securityRules[] | {name}+.properties))