0

I have the following script that 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();

This outputs the following json:

{
  "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 need to get the value of "SuspendedProcesses":[{"ProcessName": (see above)

Then if the value of "ProcessName" == "Terminate" (as it is above) do this else do this.

I know how to construct the if else syntax but how do I establish the value of "ProcessName" from the JSON output beforehand?

I also know how to manipulate an array created in a script but I'm having difficulty here because the json object is being created by the test() function so the normal rules don't seem to apply.

Any help would be appreciated. Thanks

yuriy636
  • 11,171
  • 5
  • 37
  • 42
sharksdev
  • 15
  • 4

2 Answers2

0

First, replace your console.log() calls with return statements. Then you can just do

var json = test();
var processes = json["AutoScalingGroups"][0]["Suspended Processes"]

updated code:

function test() { 
    autoscaling.describeAutoScalingGroups(params, function(err, data) { 
        if (err) {
            return [err, err.stack]; // an error occurred 
        } else {
            var json = JSON.stringify(data); // successful response 
            return json["AutoScalingGroups"][0]["Suspended Processes"];
        }
    }); 
} 
var processes = test()
console.log(processes); 
Robbie Milejczak
  • 5,664
  • 3
  • 32
  • 65
  • `json` will be `undefined` – Andreas Nov 08 '17 at 21:06
  • `First, replace your console.log() calls with return statements.` @Andreas its the first sentence – Robbie Milejczak Nov 08 '17 at 21:07
  • [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Andreas Nov 09 '17 at 04:51
  • thanks for your responses. I've updated my script with your suggestions (see below): – sharksdev Nov 09 '17 at 15:22
  • function test() { autoscaling.describeAutoScalingGroups(params, function(err, data) { if (err) return(err, err.stack); // an error occurred else return(JSON.stringify(data)); // successful response }); } var json = test(); var processes = json["AutoScalingGroups"][0]["Suspended Processes"] console.log(json); – sharksdev Nov 09 '17 at 15:22
  • @sharksdev `return()` isn't correct, return is not a function you just say `return ` I added your formatted code to my post and made that change – Robbie Milejczak Nov 09 '17 at 15:38
  • if you wouldn't mind, edit your original answer with your solution so that other people finding this with a similar problem can get help @sharksdev – Robbie Milejczak Nov 09 '17 at 15:45
  • @sharksdev can you accept my answer as correct? thanks :) – Robbie Milejczak Nov 09 '17 at 16:22
0

thanks @RobbieMilejczak, problem resolved:

First, replace your console.log() calls with return statements. Then you can just do

var json = test();
var processes = json["AutoScalingGroups"][0]["Suspended Processes"]
updated code:

function test() { 
    autoscaling.describeAutoScalingGroups(params, function(err, data) { 
        if (err) {
            return [err, err.stack]; // an error occurred 
        } else {
            var json = JSON.stringify(data); // successful response 
            return json["AutoScalingGroups"][0]["Suspended Processes"];
        }
    }); 
} 
var processes = test()
console.log(processes); 
sharksdev
  • 15
  • 4