sar
sar

Reputation: 1287

How to search on array and select the element in logic app

In logic app I have parse json data one of field is 'content' which is array of objects.

{
    "content" : [ {
            "type" : "substitution",
            "name" : "start_time",
            "value" : "4:00 p.m. PDT"
          },  {
            "type" : "substitution",
            "name" : "app_type",
            "value" : "virtual"
          }, {
            "type" : "substitution",
            "name" : "end_time",
            "value" : "4:15 p.m. PDT"
          }, {
            "type" : "substitution",
            "name" : "organization_name",
            "value" : "vivendo habitasse doctus harum platea"
          }, {
            "type" : "substitution",
            "name" : "start_date",
            "value" : "Wednesday, October 7, 2020"
          } ]

}

Now I want to search in array with name and return the value if is present else empty. e.g search by name "start_time" it should return value "4:00 p.m. PDT" How Can I achieve this in logic app ? Is there any pre defined functions that lets you search

Upvotes: 0

Views: 3359

Answers (1)

Hury Shen
Hury Shen

Reputation: 15734

As far as I know, there is no pre defined functions for us to implement this requirement. But we can implement it by writing code, please refer to my logic app below:

Prerequisites: You need to create an integration account and associate it with your logic app. You can refer to this document.(I think free pricing tier for integration account is enough)

1. Initialize a variable named inputName to store the input name (here I store app_type for test). enter image description here

2. Then initialize another variable named datasource to store the same json as yours. enter image description here

3. User "Parse JSON" action to parse datasource. enter image description here

4. After that, use JS inline code to do the search. enter image description here

var content = workflowContext.actions.Parse_JSON.outputs.body.content;

var inputName = "" + workflowContext.actions.Initialize_variable.inputs.variables[0].value;

var result = "";

content.forEach(item=>{
    if(item.name == inputName){
        result = item.value;
    }
});

return result;

5. Running the logic app, it retrieve the value virtual (which its name is app_type). enter image description here

Upvotes: 3

Related Questions