0

This is how my input looks like:

[
    {
        "Description": "Description 1"
        "OutputKey": "OutputKey 1"
        "OutputValue": "OutputValue 1"
    }
    {
        "Description": "Description 2"
        "OutputKey": "OutputKey 2"
        "OutputValue": "OutputValue 2"
    }
    {
        "Description": "Description 3"
        "OutputKey": "OutputKey 3"
        "OutputValue": "OutputValue 3"
    }
    {
        "Description": "Description 4"
        "OutputKey": "OutputKey 4"
        "OutputValue": "OutputValue 4"
    }
    {
        "Description": "Description 5"
        "OutputKey": "OutputKey 5"
        "OutputValue": "OutputValue "
    }
    {
        "Description": "Description 6"
        "OutputKey": "OutputKey 6"
        "OutputValue": "OutputValue 6"
    }
]

I need to loop through each object inside the array and fetch OutputKey and OutPutValue and use it in another function.

How do i loop through this array and get the desired values?

I am using GitBash on Windows box.

1 Answer 1

2

1) your JSON is invalid, this is a correct version (input.json) :

[
    {
        "Description": "Description 1",
        "OutputKey": "OutputKey 1",
        "OutputValue": "OutputValue 1"
    },
    {
        "Description": "Description 2",
        "OutputKey": "OutputKey 2",
        "OutputValue": "OutputValue 2"
    },
    {
        "Description": "Description 3",
        "OutputKey": "OutputKey 3",
        "OutputValue": "OutputValue 3"
    },
    {
        "Description": "Description 4",
        "OutputKey": "OutputKey 4",
        "OutputValue": "OutputValue 4"
    },
    {
        "Description": "Description 5",
        "OutputKey": "OutputKey 5",
        "OutputValue": "OutputValue "
    },
    {
        "Description": "Description 6",
        "OutputKey": "OutputKey 6",
        "OutputValue": "OutputValue 6"
    }
]

2) as far as means JavaScript Object Notation, I use with in a script :

#!/bin/bash

node<<EOF
var arr = $(cat input.json);
arr.forEach(function(el){ console.log(el.OutputKey + " " + el.OutputValue ); })
EOF

Output

OutputKey 1 OutputValue 1
OutputKey 2 OutputValue 2
OutputKey 3 OutputValue 3
OutputKey 4 OutputValue 4
OutputKey 5 OutputValue 
OutputKey 6 OutputValue 6

Note

  • you can use instead of and the print() command instead of console.log() if you prefer
  • you can use too
Sign up to request clarification or add additional context in comments.

7 Comments

The output i am receiving is from aws CLI.
Which command ? Look for app switchs, I'm pretty sure it can produce a valid JSON as well...
this is the command: echo $(aws cloudformation describe-stacks --profile myProfile --region us-east-1 --stack-name "myStack" --query 'Stacks[*].Outputs[*]' --output json)
Check docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…, you have to define a valid JSON template
I am not defining the template. I already have stack created from the template. I am just trying to grab the output values from that stack.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.