2017-05-05 43 views
0

JSON文件:如何插入匹配JSON鍵的值JSON文件,我有2間2

  • 一個模板JSON
  • 一個JSON輸出(從awscli)

模板是下面小JSON文件:

{ 
 
    "DryRun": true, 
 
    "ImageId": "", 
 
    "KeyName": "", 
 
    "SecurityGroups": [ 
 
    "" 
 
    ], 
 
    "InstanceType": "", 
 
    "Monitoring": { 
 
    "Enabled": false 
 
    }, 
 
    "SubnetId": "", 
 
    "DisableApiTermination": true, 
 
    "PrivateIpAddress": "", 
 
    "IamInstanceProfile": { 
 
    "Arn": "", 
 
    "Name": "" 
 
    }, 
 
    "EbsOptimized": true, 
 
    "TagSpecifications": [{ 
 
    "ResourceType": "", 
 
    "Tags": [{ 
 
     "Key": "", 
 
     "Value": "" 
 
    }] 
 
    }] 
 
}

原始文件的aws ec2 describe-instances輸出:

{ 
 
    "Reservations": [{ 
 
    "OwnerId": "123456789012", 
 
    "ReservationId": "r-12345678", 
 
    "Groups": [], 
 
    "Instances": [{ 
 
     "Monitoring": { 
 
     "State": "disabled" 
 
     }, 
 
     "PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com", 
 
     "RootDeviceType": "ebs", 
 
     "State": { 
 
     "Code": 16, 
 
     "Name": "running" 
 
     }, 
 
     "EbsOptimized": false, 
 
     "LaunchTime": "2016-02-09T03:06:21.000Z", 
 
     "PublicIpAddress": "12.34.56.78", 
 
     "PrivateIpAddress": "172.31.1.2", 
 
     "ProductCodes": [], 
 
     "VpcId": "vpc-1a2b3c4d", 
 
     "StateTransitionReason": "", 
 
     "InstanceId": "i-abcd1234", 
 
     "ImageId": "ami-1234abcd", 
 
     "PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal", 
 
     "KeyName": "tempKey", 
 
     "SecurityGroups": [{ 
 
      "GroupName": "somegroup1", 
 
      "GroupId": "sg-ZZZZZ" 
 
     }, 
 
     { 
 
      "GroupName": "somegroup2", 
 
      "GroupId": "sg-YYYYY" 
 
     } 
 
     ], 
 
     "ClientToken": "NutKc123456789012", 
 
     "SubnetId": "subnet-000", 
 
     "InstanceType": "t2.medium", 
 
     "NetworkInterfaces": [{ 
 
     "Status": "in-use", 
 
     "MacAddress": "02:AA:BB:CC:DD:EE", 
 
     "SourceDestCheck": true, 
 
     "VpcId": "vpc-1a2b3c4d", 
 
     "Description": "", 
 
     "Association": { 
 
      "PublicIp": "12.34.56.78", 
 
      "PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com", 
 
      "IpOwnerId": "123456789012" 
 
     }, 
 
     "NetworkInterfaceId": "eni-XXXXXXXX", 
 
     "PrivateIpAddresses": [{ 
 
      "PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal", 
 
      "Association": { 
 
      "PublicIp": "1.2.3.4", 
 
      "PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com", 
 
      "IpOwnerId": "123456789012" 
 
      }, 
 
      "Primary": true, 
 
      "PrivateIpAddress": "172.31.1.2" 
 
     }], 
 
     "PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal", 
 
     "Attachment": { 
 
      "Status": "attached", 
 
      "DeviceIndex": 0, 
 
      "DeleteOnTermination": true, 
 
      "AttachmentId": "eni-attach-XXXXXXXX", 
 
      "AttachTime": "2016-01-13T08:33:37.000Z" 
 
     }, 
 
     "Groups": [{ 
 
      "GroupName": "somegroup1", 
 
      "GroupId": "sg-ZZZZZZ" 
 
      }, 
 
      { 
 
      "GroupName": "somegroup2", 
 
      "GroupId": "sg-YYYYYY" 
 
      } 
 
     ], 
 
     "Ipv6Addresses": [], 
 
     "SubnetId": "subnet-000", 
 
     "OwnerId": "123456789012", 
 
     "PrivateIpAddress": "172.31.1.2" 
 
     }], 
 
     "SourceDestCheck": true, 
 
     "Placement": { 
 
     "Tenancy": "default", 
 
     "GroupName": "", 
 
     "AvailabilityZone": "ap-southeast-1b" 
 
     }, 
 
     "Hypervisor": "xen", 
 
     "BlockDeviceMappings": [{ 
 
     "DeviceName": "/dev/xvda", 
 
     "Ebs": { 
 
      "Status": "attached", 
 
      "DeleteOnTermination": true, 
 
      "VolumeId": "vol-33221100", 
 
      "AttachTime": "2016-01-13T08:33:39.000Z" 
 
     } 
 
     }], 
 
     "Architecture": "x86_64", 
 
     "StateReason": { 
 
     "Message": "Client.UserInitiatedShutdown: User initiated shutdown", 
 
     "Code": "Client.UserInitiatedShutdown" 
 
     }, 
 
     "RootDeviceName": "/dev/xvda", 
 
     "VirtualizationType": "hvm", 
 
     "Tags": [{ 
 
      "Value": "SomeValue", 
 
      "Key": "SomeKey" 
 
     }, 
 
     { 
 
      "Value": "AnotherValue", 
 
      "Key": "Name" 
 
     } 
 
     ], 
 
     "AmiLaunchIndex": 0 
 
    }] 
 
    }] 
 
}

我想鍵的值複製在原來的JSON文件的模板文件。

例如,KeyName是2個JSON文件之間的常用鍵。在模板文件中替換相應的值tempKey

這樣做的主要用例是:我試圖將AWS上的多個服務器從一個區域遷移到另一個區域。這是遷移過程的一部分,它將刪除AWS Console上的大量手動點擊和配置。

注意:我使用BASH命令行。

回答

1

有一種方法可以與jq做到這一點,但它不會將JSON模板作爲輸入。

您必須修改它才能成爲查詢。這不是你所需要的實際查詢,但一些讓你開始:

cat temp.json | jq '.Reservations[].Instances[] | { DryRun, ImageId, KeyName, SecurityGroups, InstanceType, Monitoring }' 

哪裏temp.json就是上面那個我放到一個文件你的輸出。對於常規的命令,只是像做aws ec2 describe-instances | jq ...

,讓我(記住限制集合我查詢了)的輸出是:

{ 
    "DryRun": null, 
    "ImageId": "ami-1234abcd", 
    "KeyName": "tempKey", 
    "SecurityGroups": [ 
    { 
     "GroupName": "somegroup1", 
     "GroupId": "sg-ZZZZZ" 
    }, 
    { 
     "GroupName": "somegroup2", 
     "GroupId": "sg-YYYYY" 
    } 
    ], 
    "InstanceType": "t2.medium", 
    "Monitoring": { 
    "State": "disabled" 
    } 
} 

希望這有助於。

+0

此外,我發現這有更多的食譜來處理'describe-instances'輸出和'jq':http://stackoverflow.com/questions/30601278/how-to-extract-keys-from-multiple -nested-arrays-using-jq – Mutahhir

+0

謝謝。會做一些修補和更新你。 – kintuparantu