Search code examples
azure-logic-apps

Fetch value from SharePoint JSON output


I am getting below JSON object from a Sharepoint list. How can I get the value for Company in the Data operation ( select) for logic apps. I did item()['Company']?['Value'] and it is not working. Any suggestions?

"body": [
  {
    "Company": {
      "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
      "Id": 0,
      "Value": "Test1"
    },
    "Date From": "2022-03-30",
    "Date To": "2022-03-31",
    "Title": "Title 1"
  },
  {
    "Company": {
      "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
      "Id": 2,
      "Value": "Line2"
    },
    "Date From": "2022-03-21",
    "Date To": "2022-03-29",
    "Title": "Title 2"
  }
]
}

I am fetching share-point list and then using data operations (select) to get the JSON as output.

I need JSON in the below format so that I can pass this to stored procedure and insert into the Azure SQL DB. I have another 12 items in the list.

         [
          {
            "Company": "Test1",
            "Date From": "2022-03-30",
            "Date To": "2022-03-31",
            "Title": "Title 1"
          },
          {
            "Company": "Line2",
            "Date From": "2022-03-21",
            "Date To": "2022-03-29",
            "Title": "Title 2"
          }
        ]

Solution

  • Rather than select, you can set a variable. We're all different but that makes far more sense to me.

    Flow

    Your expression is much the same, I used ...

    item()['Company']['Value']
    

    Just make sure you initialise the variable outside and prior to the For each ...

    Variable

    This is the result for the first item in the array ...

    Result

    To compile a full JSON object and add it to an array, again, simply use a variable and specify the values as need be.

    Firstly, initialize your array outside of the For each ...

    Array Variable

    ... and then in the For each, add an object to the array variable on each loop (make sure you include the quotes around the expression where required) ...

    Add Object to Array

    You just have to compile the JSON. The end result will look like this ...

    Result

    This is the JSON in full ...

    [
      {
        "Company": "Line2",
        "Date From": "2022-03-21",
        "Date To": "2022-03-29",
        "Title": "Title 2"
      },
      {
        "Company": "Test1",
        "Date From": "2022-03-30",
        "Date To": "2022-03-31",
        "Title": "Title 1"
      }
    ]
    

    Also, you'll notice my list has come out in a different order, that's because the For each runs in parallel, if you need to avoid that, change the settings so it runs in a single thread ...

    Concurrency

    This is the JSON definition of the LogicApp, you can load it into your tenant and test with it ...

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "For_each": {
                    "actions": {
                        "Append_to_SQL_Array": {
                            "inputs": {
                                "name": "SQL Array",
                                "value": {
                                    "Company": "@{item()['Company']['Value']}",
                                    "Date From": "@{item()['Date From']}",
                                    "Date To": "@{item()['Date To']}",
                                    "Title": "@{item()['Title']}"
                                }
                            },
                            "runAfter": {},
                            "type": "AppendToArrayVariable"
                        }
                    },
                    "foreach": "@variables('SharePoint JSON')",
                    "runAfter": {
                        "Initialize_SQL_Array": [
                            "Succeeded"
                        ]
                    },
                    "runtimeConfiguration": {
                        "concurrency": {
                            "repetitions": 1
                        }
                    },
                    "type": "Foreach"
                },
                "Initialize_SQL_Array": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "SQL Array",
                                "type": "array"
                            }
                        ]
                    },
                    "runAfter": {
                        "Initialize_SharePoint_JSON": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Initialize_SharePoint_JSON": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "SharePoint JSON",
                                "type": "array",
                                "value": [
                                    {
                                        "Company": {
                                            "Id": 0,
                                            "Value": "Test1",
                                            "odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference"
                                        },
                                        "Date From": "2022-03-30",
                                        "Date To": "2022-03-31",
                                        "Title": "Title 1"
                                    },
                                    {
                                        "Company": {
                                            "Id": 2,
                                            "Value": "Line2",
                                            "odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference"
                                        },
                                        "Date From": "2022-03-21",
                                        "Date To": "2022-03-29",
                                        "Title": "Title 2"
                                    }
                                ]
                            }
                        ]
                    },
                    "runAfter": {},
                    "type": "InitializeVariable"
                },
                "Initialize_variable": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "Result",
                                "type": "array",
                                "value": "@variables('SQL Array')"
                            }
                        ]
                    },
                    "runAfter": {
                        "For_each": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {},
            "triggers": {
                "Recurrence": {
                    "evaluatedRecurrence": {
                        "frequency": "Month",
                        "interval": 12
                    },
                    "recurrence": {
                        "frequency": "Month",
                        "interval": 12
                    },
                    "type": "Recurrence"
                }
            }
        },
        "parameters": {}
    }