2016-07-25 12 views
0

jqを使用してJSONファイルを解析しています。jqを使用して既存のファイルにjsonのセクションを追加する方法

{ 
    "Value": { 
    "Ref": "TEAM" 
    }, 
    "PropagateAtLaunch": true, 
    "Key": "TEAM" 
} 

がどのように私は、この新しいセクションを追加することができます。私は私のような別のセクションを追加したい。この中の含有量以下

[ 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TNAM" 
    }, 
    "Key": "Name" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TAPP" 
    }, 
    "Key": "application" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TENV" 
    }, 
    "Key": "environment" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TSHA" 
    }, 
    "Key": "shared" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TTER" 
    }, 
    "Key": "tier" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "CostCenter" 
    }, 
    "Key": "cost-center" 
    } 
] 

とJSONファイルのいくつかのセクションがありますか?

これは私が最初のセクションを抽出するために使用しているクエリです:

$ cat ABC.json | jq '.Resources.ASGRP.Properties.Tags' 

答えて

3

あなたは配列にオブジェクトを追加する+=を使用し、その後、--argjsonを使用してJQ変数としてオブジェクトを渡すことができます。代わりに+を使用し、あなたは元のオブジェクトをしたくない場合は

jq --argjson obj '{"sample": "object"}' '.Resources.ASGRP.Properties.Tags += [$obj]' 

+=

+0

サンティアゴありがとうございます。これは別のユースケースの実装にも役立ちました。 –

1

は、以下のように、この「jq -s add ABC.json add.json」を試してみてください。

[email protected]:/tmp$ cat add.json 
[ 
{ 
    "Value": { 
    "Ref": "TEAM" 
    }, 
    "PropagateAtLaunch": true, 
     "Key": "TEAM" 
    } 
] 
[email protected]:/tmp$ jq -s add ABC.json add.json > ABCLAST.json 
[email protected]:/tmp$ cat ABCLAST.json 
[ 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TNAM" 
    }, 
    "Key": "Name" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TAPP" 
    }, 
    "Key": "application" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TENV" 
    }, 
    "Key": "environment" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TSHA" 
    }, 
    "Key": "shared" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TTER" 
    }, 
    "Key": "tier" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "CostCenter" 
    }, 
    "Key": "cost-center" 
    }, 
    { 
    "PropagateAtLaunch": true, 
    "Value": { 
     "Ref": "TEAM" 
    }, 
    "Key": "TEAM" 
    } 
] 
+0

ありがとうDogru。これは私を大いに助けました。 –

関連する問題