2016-10-11 8 views
0

json 구조를 간단한 객체 배열로 변환하려면 'jq'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": "*" 
} 
] 

나는 방법을 많이 시도했지만 아무것도 할 수 없습니다.

도움을 주셔서 감사합니다. 마지막 두 줄의 반복을 피하기 위해 원하는 경우에 당신은과 마지막 네 줄을 대체 할 수있는,

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

:

답변

0

쉽게 읽을 여기를 배치 다음 필터는 요청에 따라 입력을 집계합니다 :

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

은 훌륭한 솔루션을 주셔서 감사합니다! –

1

다른 솔루션입니다 :

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