Search code examples
arraysazureazure-data-factory

Append child to existing array in Azure Data Factory


I have a json array (as a result of an activity):

{
    "name": "Union",
    "value": [
        {
            "branch": [
                {
                    "origin": "CAPUA",
                    "engineId": 398001,
                    "registration": "WEOIWEIJOKDFIJ09092309",
                    
                    }
                }
            ]
        },
    
    ]
}

I want to append to this existing array a new child element filterId and its value. For this purpose I have used a SerVariable, with the following formula:@union(array(activity('GetEngines').output),array(concat('"filterId":',variables('fltr')))) However the output I get is :

{
    "name": "Union",
    "value": [
        {
            "branch": [
                {
                    "origin": "CAPUA",
                    "engineId": 398001,
                    "registration": "WEOIWEIJOKDFIJ09092309",
                    
                    }
                }
            ]
        },
        "\"filterId\":398291"
    ]
}

Which is not what I am looking for. I want to insert the "filterId":398291 underneath "engineId": 398001, in the existing array. Does anyone have an idea on how this could be achieved easier?

Thank you!


Solution

  • It looks like you're trying to add a new key-value pair ("filterId": 398291) to the existing JSON structure under the "branch" array. The issue is with the way you're using the concat function. Instead of appending a new array element, it's adding a string element to the array.

    To achieve what you want, you can use the following approach: { "name": "Union", "value": [ { "branch": [ { "origin": "CAPUA", "engineId": 398001, "registration": "WEOIWEIJOKDFIJ09092309", "_elements": { "self": { "href": "https://api.tttttt.com/CAPUA/398001" }, "children": { "href": "https://api.tttttt.com/CAPUA/398001/children" }, "entity": { "href": "https://api.tttttt.com/CAPUA/398001" }, "filterId": 398291 } } ] } ] } It looks like you're trying to add a new key-value pair ("filterId": 398291) to the existing JSON structure under the "branch" array. The issue is with the way you're using the concat function. Instead of appending a new array element, it's adding a string element to the array.

    To achieve what you want, you can use the following approach:

    json

    { "name": "Union", "value": [ { "branch": [ { "origin": "CAPUA", "engineId": 398001, "registration": "WEOIWEIJOKDFIJ09092309", "_elements": { "self": { "href": "https://api.tttttt.com/CAPUA/398001" }, "children": { "href": "https://api.tttttt.com/CAPUA/398001/children" }, "entity": { "href": "https://api.tttttt.com/CAPUA/398001" }, "filterId": 398291 } } ] } ] }

    Assuming you have a variable named fltr with the value 398291, you can use the following formula to achieve the desired result: @union( array( activity('GetEngines').output ), array( json( '{ "branch": [ { "_elements": { "filterId": ' + variables('fltr') } } ] }' ) ) ) This formula uses the json function to create a JSON structure for the new element with "filterId" and its value, and then uses @union to combine it with the existing array obtained from the 'GetEngines' activity. This should add the new element as a child of the existing "branch" array.