0

I am creating a Logic app to gather members from one platform using an API call and posting them to another platform using POST method. At the end of the entire process, I get a JSON array with the data that I need. However, I need to add in a parameters into the array at the beginning. How would I go about doing so?

Currently, my array looks like this

[
  {
    "company": "",
    "email": "",
    "firstName": "",
    "lastName": "",
    "nickname": "",
    "prefix": "",
    "sourceId": "",
    "title": "",
    "workPhone": ""
  },
  {
    "company": "",
    "email": "",
    "firstName": "",
    "lastName": "",
    "nickname": "",
    "prefix": "",
    "sourceId": "",
    "title": "",
    "workPhone": ""
  }
]

I need for the body of my HTTP request to look like this:

**{"data":**
    [
     **"dataRecord":** {
        "company": "",
        "email": "",
        "firstName": "",
        "lastName": "",
        "nickname": "",
        "prefix": "",
        "sourceId": "",
        "title": "",
        "workPhone": ""
      },
      {
        "company": "",
        "email": "",
        "firstName": "",
        "lastName": "",
        "nickname": "",
        "prefix": "",
        "sourceId": "",
        "title": "",
        "workPhone": ""
      }
    }

My current flow looks like this:

  • Scheduled Trigger

  • List item

  • Authenticate platform (to)

  • Authentication platform(from)

  • Get Data

  • Compose data

  • Parse Json

  • Initialize Array Variable

  • For Each: (1)Compose - Map Parsed JSON data to Destination Fields (2)Append to array variable

  • compose expression: string(variables('variable'))

  • Compose string to Json: json(string(outputs('Compose_2')))

  • HTTP POST

Edit: Adding screenshot of where I need the data to be in the output, along with what my app looks like

JSON Output after for each

App layout

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Basilio
  • 1
  • 2

1 Answers1

1

After receiving the json array try using Parse Json action of Data Operations Connector to get the parameters and then you can form a json using Compose Connector. Here is the screenshot of my logic app for your reference.

enter image description here

Here is the output:

enter image description here

enter image description here

Here is the schema in the compose connector

{
  "data": [
    {
      "dataRecord": {
        "company": "@{items('For_each')['company']}",
        "email": "@{items('For_each')['email']}",
        "firstName": "@{items('For_each')['firstName']}",
        "lastName": "@{items('For_each')['lastName']}",
        "nickname": "@{items('For_each')['nickname']}",
        "prefix": "@{items('For_each')['prefix']}",
        "sourceId": "@{items('For_each')['sourceId']}",
        "title": "@{items('For_each')['title']}",
        "workPhone": "@{items('For_each')['workPhone']}"
      }
    }
  ]
}

Below is the code view of my logic app

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Compose": {
                        "inputs": {
                            "data": [
                                {
                                    "dataRecord": {
                                        "company": "@{items('For_each')['company']}",
                                        "email": "@{items('For_each')['email']}",
                                        "firstName": "@{items('For_each')['firstName']}",
                                        "lastName": "@{items('For_each')['lastName']}",
                                        "nickname": "@{items('For_each')['nickname']}",
                                        "prefix": "@{items('For_each')['prefix']}",
                                        "sourceId": "@{items('For_each')['sourceId']}",
                                        "title": "@{items('For_each')['title']}",
                                        "workPhone": "@{items('For_each')['workPhone']}"
                                    }
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "Compose"
                    }
                },
                "foreach": "@body('Parse_JSON')",
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@triggerBody()",
                    "schema": {
                        "items": {
                            "properties": {
                                "company": {
                                    "type": "string"
                                },
                                "email": {
                                    "type": "string"
                                },
                                "firstName": {
                                    "type": "string"
                                },
                                "lastName": {
                                    "type": "string"
                                },
                                "nickname": {
                                    "type": "string"
                                },
                                "prefix": {
                                    "type": "string"
                                },
                                "sourceId": {
                                    "type": "string"
                                },
                                "title": {
                                    "type": "string"
                                },
                                "workPhone": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "company",
                                "email",
                                "firstName",
                                "lastName",
                                "nickname",
                                "prefix",
                                "sourceId",
                                "title",
                                "workPhone"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    }
                },
                "runAfter": {},
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}
SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18
  • Thank you for the response. If I add those items in the For Each compose, I will then have those data and Datarecord items repeated each time. – Basilio Mar 01 '22 at 14:15
  • I added images to my original post with some images. – Basilio Mar 01 '22 at 14:22
  • @Basilio The for each loop in here will point to the next array present in the input of parse json. so to not repeat the items each time you just need to write the json for one array. If you use the one that you have mentioned in the question you would probably receive duplicates. Can you try using the one which I have mentioned in the answer. – SwethaKandikonda Mar 01 '22 at 14:24
  • Understood! Thank you, let me try that. – Basilio Mar 01 '22 at 14:33
  • I have used expressions to take the output of the Array variable and convert it to string. I then use the replace expression to add in the items that I need. However, Microsoft keeps adding in the escape \slashes. I can then use the Json conversion function to convert it back to json, but it removes the data I have added. @swethakandikonda-mt "{\"data\": [ \"dataRecord\":{\"company\":\"... – Basilio Mar 01 '22 at 14:55
  • @Basilio usually when converting data into string in logic apps results something like this. Would you mind adding the screenshot of the updated logic app. – SwethaKandikonda Mar 01 '22 at 14:58
  • @Basilio Is your issue resolved? – SwethaKandikonda Mar 02 '22 at 11:57
  • I apologize, I have not resolved the issue. To add a screenshot, do I always edit my original post to add it? I can't seem to add it via comment. – Basilio Mar 03 '22 at 13:13
  • @Basilio, yes, you can edit the original post to add screenshots or you can use image url to add image to the comments – SwethaKandikonda Mar 03 '22 at 15:25
  • I have not had a chance to continue on the project. I will retry shortly and confirm. Thanks again for your help. – Basilio Mar 15 '22 at 12:28