V.V
V.V

Reputation: 3094

Logic app : How to merge multiple response using each loop

I'm using 1 3rd party service which provides me 500 records at a time, but I want all records together in single request. I've integrated that part using Each loop enter image description here this is how my for each loop looks like

When I tried to fetch all response using Array, the data is not strucutred, Array appends whole JSON which of response as object which looks like this

[{
    "total": 628,
    "start": 0,
    "count": 500,
    "data": [
        {
object 1
        }]
},
{
    "total": 628,
    "start": 0,
    "count": 500,
    "data": [
        {
object 2
        }]
}]

Now what I want is

[{
    "total": 628,
    "start": 0,
    "count": 500,
    "data": [
        {
 object 1
        },
        {
object 2
        },
        {
object 3
         }
}]

I have no idea how to merge this data in logic app, can some one please guide how to do this

Upvotes: 0

Views: 4433

Answers (1)

George Chen
George Chen

Reputation: 14334

You don't show details about your flow, so maybe you could refer to my below flow. To mock you situation, I use http trigger to get the json data. Be low is my test json data. If this is not what you want please feel free to let me know.

[{
    "total": 628,
    "start": 0,
    "count": 500,
    "data": [
        {
      "count": 0,
      "lookup": "PSI"
    }]
},
{
    "total": 628,
    "start": 0,
    "count": 500,
    "data": [
        {
      "count": 1,
      "lookup": "CLEAN"
    }]
}]

Firstly I parse the json data from the trigger body, the initiate a array variable to store the array after formatting.

enter image description here

Then loop the json data, formatting the array. If all your data property only have one object json in the array, you could append the array with items('For_each')['data'][0], if not you need another For each to loop the data array.

enter image description here

Then get the result json value you want, cause you just want one json object and your json values are same except the data property. So I use a compose action to henerate the json. I set the value with setProperty(body('Parse_JSON')[0],'data',variables('array')).

enter image description here

Here is my test result, hope this is what you want.

enter image description here

Update: If I didn't misunderstand, your data array has more than one object and you want to get them all, If right just like have mentioned you need another For each action to loop the array then append all of them to the variable, you could refer to below flow.

All other actions are same, add a For each in the exist For each.

enter image description here

Here is my test result. Below is the json data I sent to logic app.

[{
    "total": 628,
    "start": 0,
    "count": 500,
    "data": [
        {
      "count": 0,
      "lookup": "PSI"
    },{
        "count": 3,
      "lookup": "TEST"
    }]
},
{
    "total": 628,
    "start": 0,
    "count": 500,
    "data": [
        {
      "count": 1,
      "lookup": "CLEAN"
    },{
        "count": 4,
      "lookup": "GET"
    }]
}]

enter image description here

Update2: Below is my test json and the result:

[{ 
    "total": 628,
    "start": 0,
    "count": 500,
    "data": [ 
        [
            { "count" : 0, "lookup": "test0" },
            { "count": 1, "lookup": "test1" }
        ], 
        [ 
            { "count": 2, "lookup": "test2" },
            { "count": 3, "lookup": "test3" }
        ]
    ] 
},{ 
    "total": 628,
    "start": 0,
    "count": 500,
    "data": [ 
        [
            { "count" : 4, "lookup": "test4" },
            { "count": 5, "lookup": "test5" }
        ], 
        [ 
            { "count": 6, "lookup": "test6" },
            { "count": 7, "lookup": "test7" }
        ]
    ] 
}]

Just add one more for each action.

enter image description here

enter image description here

Update: to send the http response with json data, you could use Response action to do it.

enter image description here

enter image description here

Upvotes: 2

Related Questions