Parse JSON Body with same object names then create variables

Network Engineer in an early learning state.

I am trying to create a workflow where I do a GET on an API and want to parse the body for a specific key value pair and store that as a variable. Seems easy enough but the hard part (at least for me) is the GET returns several objects with the same key name and is represented several times throughout the body.

Example:
{
“Rules”: [
{
“ruleId”: ABC123,
“ruleName”: “Alert Rule A”
},
{
“ruleId”: CDF456,
“ruleName”: “Alert Rule B”
},
{
“ruleId”: EFG789,
“ruleName”: “Alert Rule C”
},
]
}

What I would love to do here is parse the body for “Alert Rule C” and set a variable for ruleID = EFG789. The “ruleName” is a constant, the “ruleID” changes. So I know I can always match “ruleName”. In a perfect state, I want to do this to 5 different objects in the body and create 5 variables I can call in subsequent POST requests.

If someone can point me in the right direction Im sure I can figure it out, I just have not had a ton of luck googling with my search terms. Thank you!

let body = {
    "Rules": [
        {
            "ruleId": "ABC123",
            "ruleName": "Alert Rule A"
        },
        {
            "ruleId": "CDF456",
            "ruleName": "Alert Rule B"
        },
        {           
            "ruleId": "EFG789",
            "ruleName": "Alert Rule C"
        },
    ]
}

let search = (body.Rules.find(obj => {return obj.ruleName === 'Alert Rule C'})).ruleId; 
//EFG789

console.log(search);

It might be worth breaking down that query so its more understandable.

The JavaScript find function works against arrays.

The array in your example response is defined under the “Rules” key.

The find command must come straight after the array, so body.Rules.find.

Your example has an array of JavaScript objects. Each ruleID and ruleName is in a separate object.

Therefore you can search for any of the keys within the object.

obj => {return obj.ruleName === ‘Alert Rule C’})

And finally we just want the ruleId.