arraysobjectforeachazure-logic-apps

Azure Logic App - For Each Key/Value Pair (Answered!)


Community!

I'm trying to iterate (for each) using logic apps and I want to be able able to set the KEY (Currency Code) as a variable and the Value (Currency Exchanged) as a variable. I'm doing a parse json action using the body of the api call

{
"type": "array",
"items": {
    "type": "object",
    "properties": {
        "AED": {
            "type": "number"
        },
        "AFN": {
            "type": "number"
        },
        "ALL": {
            "type": "number"
        },
        "AMD": {
            "type": "number"
        },
        "ANG": {
            "type": "number"
        },
        "AOA": {
            "type": "number"
        },
        "ARS": {
            "type": "number"
        },
        "AUD": {
            "type": "number"
        }}}

Then I have the body of the parse JSON

 [{
"AED": 2.473902,
"AFN": 47.309894,
"ALL": 60.509834,
"AMD": 260.86733,
"ANG": 1.213412,
"AOA": 620.003118,
"ARS": 642.565513,
"AUD": 1 
}]

I just can't for the life of me get the for each to work, does anyone have any ideas?


Solution

  • So what I ended up doing was using a simple JavaScript code using Inline Code and it worked perfectly!

    // Input JSON data
    var data = workflowContext.actions.Parse_JSON.outputs.body
    
    // Initialize array to store key-value pairs
    var keyValuePairs = [];
    
    // Check if data is not empty
    if (data.length > 0) {
        var currencies = data[0]; // Since there's only one object in the array
    
        for (var key in currencies) {
            if (currencies.hasOwnProperty(key)) {
                keyValuePairs.push({ key: key, value: currencies[key] });
            }
        }
    }
    
    // Return the key-value pairs
    return keyValuePairs;