从 SharePoint JSON 输出中获取值

fetch value from SharePoint JSON output

我正在从共享点列表中获取以下 JSON 对象。如何在逻辑应用程序的数据操作 (select) 中获取 Company 的值。我做了 item()['Company']?['Value'] 但它不起作用。有什么建议吗?

"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"
  }
]
}

我正在获取共享点列表,然后使用数据操作 (select) 获取 JSON 作为输出。

我需要以下格式的 JSON,以便我可以将其传递给存储过程并插入到 Azure SQL 数据库中。我在列表中还有另外 12 个项目。

         [
          {
            "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"
          }
        ]

而不是select,您可以设置一个变量。我们都是不同的,但这对我来说更有意义。

你的表达方式大同小异,我用.​​..

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

只需确保在 For each ...

之外和之前初始化变量

这是数组中第一项的结果...

要编译一个完整的 JSON 对象并将其添加到一个数组中,再次简单地使用一个变量并根据需要指定值。

首先,在 For each ...

之外初始化数组

... 然后在 For each 中,在每个循环中向数组变量添加一个对象(确保在需要的地方包括表达式周围的引号)...

你只需要编译JSON。最终结果将如下所示...

这是完整的 JSON ...

[
  {
    "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"
  }
]

此外,您会注意到我的列表以不同的顺序出现,那是因为 For each 并行运行,如果您需要避免这种情况,请更改设置以使其在单个线程中运行...

这是 LogicApp 的 JSON 定义,您可以将其加载到您的租户中并使用它进行测试...

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