2017-05-13 45 views
1

我想向文件中的每個json對象添加父項。 我的出發點是包含兩個JSON項目的下列JSON文件:使用jq/bash向json對象添加父元素

{ 
    "id": { 
    "S": "cf7ebec368f241ead7ecf818ce9ed098406afa63" 
    }, 
    "test": { 
    "N": "5" 
    }, 
    "added": { 
    "S": "2017-02-15T17:56:19.958917+00:00" 
    }, 
    "foo": { 
    "N": "88" 
    }, 
    "web": { 
    "N": "103" 
    } 
} 
{ 
    "id": { 
    "S": "cf7ebec368f241ead7ecf818ce9ed098406afa63" 
    }, 
    "image_server_id": { 
    "N": "5" 
    }, 
    "added": { 
    "S": "2017-02-15T17:56:19.958917+00:00" 
    }, 
    "result": { 
    "N": "88" 
    }, 
    "data": { 
    "foo": { 
     "N": "103", 
     "S": "test" 
    } 
    } 
} 

使用JQ和/或bash我想生成以下JSON文件:

{ 
    "*StaticString*": [ 
    { 
     "PutRequest": { 
      "Item": { 
       "id": { 
       "S": "cf7ebec368f241ead7ecf818ce9ed098406afa63" 
       }, 
       "test": { 
       "N": "5" 
       }, 
       "added": { 
       "S": "2017-02-15T17:56:19.958917+00:00" 
       }, 
       "foo": { 
       "N": "88" 
       }, 
       "web": { 
       "N": "103" 
       } 
      **} 
     } 
    }, 
    { 
     "PutRequest": { 
      "Item": { 
       "id": { 
       "S": "cf7ebec368f241ead7ecf818ce9ed098406afa63" 
       }, 
       "image_server_id": { 
       "N": "5" 
       }, 
       "added": { 
       "S": "2017-02-15T17:56:19.958917+00:00" 
       }, 
       "result": { 
       "N": "88" 
       }, 
       "data": { 
       "foo": { 
        "N": "103", 
        "S": "test" 
       } 
       } 
      **} 
     } 
    } 
    ] 
    } 

總之我要添加

{ 
"StaticString": [ 
{ 

在文件的開頭。 然後我需要把每一個JSON項目到父

"PutRequest": { 
      "Item": { 
      ... 
      } 
    } 

,並生成一個數組了JSON項目。

我已經知道如何使用 jq -s . testfile.json 生成JSON項目的數組,但我不知道如何父添加到每個JSON項目。

我希望很清楚我想達到什麼目的。

感謝的對你有所幫助, 克里斯

回答

0

嘗試

  •  
    jq -s '{staticstring:[{PutRequest:{Item:.[]}}]}' inputfile.json

+0

感謝的!它的作品很棒:) – bsj4sla