I have a custom script that generates and outputs a JSON formatted object:
function test() {
autoscaling.describeAutoScalingGroups(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(JSON.stringify(data)); // successful response
});
}
test();
The resulting JSON content is as follows:
{
"ResponseMetadata": {
"RequestId": "##################"
},
"AutoScalingGroups": [
{
"AutoScalingGroupName": "################",
"AutoScalingGroupARN": "arn:aws:autoscaling:eu-west-1:#########:autoScalingGroup:###########",
"LaunchConfigurationName": "######-LC-###########",
"MinSize": 0,
"MaxSize": 0,
"DesiredCapacity": 0,
"DefaultCooldown": 300,
"AvailabilityZones": [
"eu-west-1b",
"eu-west-1c",
"eu-west-1a"
],
"LoadBalancerNames": [
"#########-ELB-###########"
],
"TargetGroupARNs": [
],
"HealthCheckType": "ELB",
"HealthCheckGracePeriod": 300,
"Instances": [
],
"CreatedTime": "2017-11-08T18:22:05.093Z",
"SuspendedProcesses": [
{
"ProcessName": "Terminate",
"SuspensionReason": "User suspended at 2017-11-08T18:22:14Z"
}
],
"VPCZoneIdentifier": "subnet-######,subnet-#######,subnet-#######",
"EnabledMetrics": [
],
"Tags": [
{
"ResourceId": "#######-ASG-##########",
"ResourceType": "auto-scaling-group",
"Key": "aws:cloudformation:logical-id",
"Value": "ASG",
"PropagateAtLaunch": true
},
{
"ResourceId": "#######-ASG-#########",
"ResourceType": "auto-scaling-group",
"Key": "aws:cloudformation:stack-id",
"Value": "arn:aws:cloudformation:eu-west-1:########:stack/##############",
"PropagateAtLaunch": true
},
{
"ResourceId": "################",
"ResourceType": "auto-scaling-group",
"Key": "aws:cloudformation:stack-name",
"Value": "#######",
"PropagateAtLaunch": true
}
],
"TerminationPolicies": [
"Default"
],
"NewInstancesProtectedFromScaleIn": false
}
]
}
I am seeking to extract the value of
"SuspendedProcesses":[{"ProcessName"
: (refer above)
If the value of "ProcessName" == "Terminate"
(as seen above), I want to execute one set of instructions; otherwise, another.
I understand how to structure the if-else statement, but I'm unsure about obtaining the value of "ProcessName"
from the JSON output in advance.
While I confidently handle arrays created within a script, I'm encountering difficulty in this context because the JSON object is generated by the test()
function, so conventional rules seem not to apply.
Your guidance on this matter would be greatly appreciated. Thank you.