简体   繁体   中英

fetch value from SharePoint JSON output

I am getting below JSON object from 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 store proc 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"
          }
        ]

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

流

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 ...

多变的

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

结果

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 ...

数组变量

... 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)...

将对象添加到数组

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

结果

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...

并发

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": {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM