2016-10-11 5 views
0

Я хотел бы использовать процессор jq 'json для преобразования структуры json в массив простых объектов.Создать массив из многомерного с помощью 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 }) 

Если вы хотите, чтобы избежать повторения в двух последних строках, вы можете заменить последние четыре строки с :

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

Спасибо, отличное решение! –

1

Вот еще одно решение:

.nsgs | map({comments} + (.properties.securityRules[] | {name}+.properties)) 
Смежные вопросы