复杂 JSON 和逻辑应用程序的挑战
Challenges with complex JSON and Logic App
我有一个逻辑应用程序的挑战,但我无法解决。该应用程序的想法是
- 将作为二进制文件存储的 XML 文件存储到 blob 存储并 JSON 对其进行验证(解析 JSON 操作)
- 遍历 JSON 中的“值”数组并在每一轮中添加一个具有特定值的列(撰写操作)并将输出附加到数组变量中
- 将数组变量存储到 CSV(创建 CSV table 操作)并将文件上传到数据湖
使用简单的 JSON 这很容易,但在我们的例子中,架构非常复杂。
第一步似乎有效,因为我们得到一个 JSON,其中包含我们在数组“Values”中需要的内容。但是这里的模式可能需要一些调整,因为我们 运行 稍后会遇到问题。结果 JSON 在下方。
我 运行 遇到问题的地方是第二步:
在这里我得到以下错误:
表达式评估失败。执行模板操作 'For_each' 失败:'foreach' 表达式的计算结果 '@body('Parse_JSON')?['diffgr:diffgram']?['Results'] ?['Values']' 的类型为 'Null'。结果必须是一个有效的数组。
我也尝试将值读入数组变量,但它只是确认我们确实没有从 JSON 中的“值”标签下获取实际数据行,因为输出只是空括号:
下面是解析 JSON 操作的详细信息以及生成的 JSON 以帮助理解问题。
解析JSON动作
内容:Json(Xml(base64ToString(body('Get_blob_content')?['$content'])))
架构:
{
"properties": {
"diffgr:diffgram": {
"properties": {
"@@xmlns:diffgr": {
"type": "string"
},
"@@xmlns:msdata": {
"type": "string"
},
"Results": {
"properties": {
"Properties": {
"properties": {
"@@diffgr:id": {
"type": "string"
},
"@@msdata:rowOrder": {
"type": "string"
},
"@@stepped": {
"type": "string"
}
},
"type": "object"
},
"Values": {
"items": {
"properties": {
"@@diffgr:id": {
"type": "string"
},
"@@msdata:rowOrder": {
"type": "string"
},
"@@status": {
"type": "string"
},
"@@ts": {
"type": "string"
},
"@@value": {
"type": "string"
}
},
"required": [
"@@diffgr:id",
"@@msdata:rowOrder",
"@@ts",
"@@value",
"@@status"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
},
"xs:schema": {
"properties": {
"@@id": {
"type": "string"
},
"@@xmlns": {
"type": "string"
},
"@@xmlns:msdata": {
"type": "string"
},
"@@xmlns:xs": {
"type": "string"
},
"xs:element": {
"properties": {
"@@msdata:IsDataSet": {
"type": "string"
},
"@@msdata:Locale": {
"type": "string"
},
"@@name": {
"type": "string"
},
"xs:complexType": {
"properties": {
"xs:choice": {
"properties": {
"@@maxOccurs": {
"type": "string"
},
"@@minOccurs": {
"type": "string"
},
"xs:element": {
"items": {
"properties": {
"@@msdata:Locale": {
"type": "string"
},
"@@name": {
"type": "string"
},
"xs:complexType": {
"properties": {
"xs:attribute": {
"items": {
"properties": {
"@@msdata:Caption": {
"type": "string"
},
"@@name": {
"type": "string"
},
"@@type": {
"type": "string"
}
},
"required": [
"@@name",
"@@msdata:Caption",
"@@type"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"required": [
"@@name",
"@@msdata:Locale",
"xs:complexType"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
JSON 我们从 Parse JSON
得到结果
{
"body": {
"?xml": {
"@version": "1.0",
"@encoding": "utf-8"
},
"DataSet": {
"xs:schema": {
"@id": "Results",
"@xmlns": "",
"@xmlns:xs": "http://www.w3.org/2001/XMLSchema",
"@xmlns:msdata": "urn:schemas-microsoft-com:xml-msdata",
"xs:element": {
"@name": "Results",
"@msdata:IsDataSet": "true",
"@msdata:Locale": "",
"xs:complexType": {
"xs:choice": {
"@minOccurs": "0",
"@maxOccurs": "unbounded",
"xs:element": [
{
"@name": "Values",
"@msdata:Locale": "",
"xs:complexType": {
"xs:attribute": [
{
"@name": "ts",
"@msdata:Caption": "81000-F901C Trend ts (81000-F901C)",
"@type": "xs:dateTime"
},
{
"@name": "value",
"@msdata:Caption": "81000-F901C Trend value (81000-F901C)",
"@type": "xs:float"
},
{
"@name": "status",
"@msdata:Caption": "81000-F901C Trend status (81000-F901C)",
"@type": "xs:string"
}
]
}
},
{
"@name": "Properties",
"@msdata:Locale": "",
"xs:complexType": {
"xs:attribute": {
"@name": "stepped",
"@type": "xs:int"
}
}
}
]
}
}
}
},
"diffgr:diffgram": {
"@xmlns:msdata": "urn:schemas-microsoft-com:xml-msdata",
"@xmlns:diffgr": "urn:schemas-microsoft-com:xml-diffgram-v1",
"Results": {
"Values": [
{
"@diffgr:id": "Values1",
"@msdata:rowOrder": "0",
"@ts": "2020-09-02T00:00:00+03:00",
"@value": "1.41233063",
"@status": "Good"
},
{
"@diffgr:id": "Values2",
"@msdata:rowOrder": "1",
"@ts": "2020-09-02T01:00:00+03:00",
"@value": "1.5102241",
"@status": "Good"
},
{
"@diffgr:id": "Values3",
"@msdata:rowOrder": "2",
"@ts": "2020-09-02T02:00:00+03:00",
"@value": "1.18518186",
"@status": "Good"
}
],
"Properties": {
"@diffgr:id": "Properties1",
"@msdata:rowOrder": "0",
"@stepped": "0"
}
}
}
}
}
}
看来你的schema
有问题,请用这个schema
:
{
"properties": {
"body": {
"properties": {
"?xml": {
"properties": {
"@@encoding": {
"type": "string"
},
"@@version": {
"type": "string"
}
},
"type": "object"
},
"DataSet": {
"properties": {
"diffgr:diffgram": {
"properties": {
"@@xmlns:diffgr": {
"type": "string"
},
"@@xmlns:msdata": {
"type": "string"
},
"Results": {
"properties": {
"Properties": {
"properties": {
"@@diffgr:id": {
"type": "string"
},
"@@msdata:rowOrder": {
"type": "string"
},
"@@stepped": {
"type": "string"
}
},
"type": "object"
},
"Values": {
"items": {
"properties": {
"@@diffgr:id": {
"type": "string"
},
"@@msdata:rowOrder": {
"type": "string"
},
"@@status": {
"type": "string"
},
"@@ts": {
"type": "string"
},
"@@value": {
"type": "string"
}
},
"required": [
"@@diffgr:id",
"@@msdata:rowOrder",
"@@ts",
"@@value",
"@@status"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
},
"xs:schema": {
"properties": {
"@@id": {
"type": "string"
},
"@@xmlns": {
"type": "string"
},
"@@xmlns:msdata": {
"type": "string"
},
"@@xmlns:xs": {
"type": "string"
},
"xs:element": {
"properties": {
"@@msdata:IsDataSet": {
"type": "string"
},
"@@msdata:Locale": {
"type": "string"
},
"@@name": {
"type": "string"
},
"xs:complexType": {
"properties": {
"xs:attribute": {
"items": {
"properties": {
"@@msdata:Caption": {
"type": "string"
},
"@@name": {
"type": "string"
},
"@@type": {
"type": "string"
}
},
"required": [
"@@name",
"@@msdata:Caption",
"@@type"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
我有一个逻辑应用程序的挑战,但我无法解决。该应用程序的想法是
- 将作为二进制文件存储的 XML 文件存储到 blob 存储并 JSON 对其进行验证(解析 JSON 操作)
- 遍历 JSON 中的“值”数组并在每一轮中添加一个具有特定值的列(撰写操作)并将输出附加到数组变量中
- 将数组变量存储到 CSV(创建 CSV table 操作)并将文件上传到数据湖
使用简单的 JSON 这很容易,但在我们的例子中,架构非常复杂。
第一步似乎有效,因为我们得到一个 JSON,其中包含我们在数组“Values”中需要的内容。但是这里的模式可能需要一些调整,因为我们 运行 稍后会遇到问题。结果 JSON 在下方。
我 运行 遇到问题的地方是第二步:
在这里我得到以下错误: 表达式评估失败。执行模板操作 'For_each' 失败:'foreach' 表达式的计算结果 '@body('Parse_JSON')?['diffgr:diffgram']?['Results'] ?['Values']' 的类型为 'Null'。结果必须是一个有效的数组。
我也尝试将值读入数组变量,但它只是确认我们确实没有从 JSON 中的“值”标签下获取实际数据行,因为输出只是空括号:
下面是解析 JSON 操作的详细信息以及生成的 JSON 以帮助理解问题。
解析JSON动作
内容:Json(Xml(base64ToString(body('Get_blob_content')?['$content'])))
架构:
{
"properties": {
"diffgr:diffgram": {
"properties": {
"@@xmlns:diffgr": {
"type": "string"
},
"@@xmlns:msdata": {
"type": "string"
},
"Results": {
"properties": {
"Properties": {
"properties": {
"@@diffgr:id": {
"type": "string"
},
"@@msdata:rowOrder": {
"type": "string"
},
"@@stepped": {
"type": "string"
}
},
"type": "object"
},
"Values": {
"items": {
"properties": {
"@@diffgr:id": {
"type": "string"
},
"@@msdata:rowOrder": {
"type": "string"
},
"@@status": {
"type": "string"
},
"@@ts": {
"type": "string"
},
"@@value": {
"type": "string"
}
},
"required": [
"@@diffgr:id",
"@@msdata:rowOrder",
"@@ts",
"@@value",
"@@status"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
},
"xs:schema": {
"properties": {
"@@id": {
"type": "string"
},
"@@xmlns": {
"type": "string"
},
"@@xmlns:msdata": {
"type": "string"
},
"@@xmlns:xs": {
"type": "string"
},
"xs:element": {
"properties": {
"@@msdata:IsDataSet": {
"type": "string"
},
"@@msdata:Locale": {
"type": "string"
},
"@@name": {
"type": "string"
},
"xs:complexType": {
"properties": {
"xs:choice": {
"properties": {
"@@maxOccurs": {
"type": "string"
},
"@@minOccurs": {
"type": "string"
},
"xs:element": {
"items": {
"properties": {
"@@msdata:Locale": {
"type": "string"
},
"@@name": {
"type": "string"
},
"xs:complexType": {
"properties": {
"xs:attribute": {
"items": {
"properties": {
"@@msdata:Caption": {
"type": "string"
},
"@@name": {
"type": "string"
},
"@@type": {
"type": "string"
}
},
"required": [
"@@name",
"@@msdata:Caption",
"@@type"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"required": [
"@@name",
"@@msdata:Locale",
"xs:complexType"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
JSON 我们从 Parse JSON
得到结果{
"body": {
"?xml": {
"@version": "1.0",
"@encoding": "utf-8"
},
"DataSet": {
"xs:schema": {
"@id": "Results",
"@xmlns": "",
"@xmlns:xs": "http://www.w3.org/2001/XMLSchema",
"@xmlns:msdata": "urn:schemas-microsoft-com:xml-msdata",
"xs:element": {
"@name": "Results",
"@msdata:IsDataSet": "true",
"@msdata:Locale": "",
"xs:complexType": {
"xs:choice": {
"@minOccurs": "0",
"@maxOccurs": "unbounded",
"xs:element": [
{
"@name": "Values",
"@msdata:Locale": "",
"xs:complexType": {
"xs:attribute": [
{
"@name": "ts",
"@msdata:Caption": "81000-F901C Trend ts (81000-F901C)",
"@type": "xs:dateTime"
},
{
"@name": "value",
"@msdata:Caption": "81000-F901C Trend value (81000-F901C)",
"@type": "xs:float"
},
{
"@name": "status",
"@msdata:Caption": "81000-F901C Trend status (81000-F901C)",
"@type": "xs:string"
}
]
}
},
{
"@name": "Properties",
"@msdata:Locale": "",
"xs:complexType": {
"xs:attribute": {
"@name": "stepped",
"@type": "xs:int"
}
}
}
]
}
}
}
},
"diffgr:diffgram": {
"@xmlns:msdata": "urn:schemas-microsoft-com:xml-msdata",
"@xmlns:diffgr": "urn:schemas-microsoft-com:xml-diffgram-v1",
"Results": {
"Values": [
{
"@diffgr:id": "Values1",
"@msdata:rowOrder": "0",
"@ts": "2020-09-02T00:00:00+03:00",
"@value": "1.41233063",
"@status": "Good"
},
{
"@diffgr:id": "Values2",
"@msdata:rowOrder": "1",
"@ts": "2020-09-02T01:00:00+03:00",
"@value": "1.5102241",
"@status": "Good"
},
{
"@diffgr:id": "Values3",
"@msdata:rowOrder": "2",
"@ts": "2020-09-02T02:00:00+03:00",
"@value": "1.18518186",
"@status": "Good"
}
],
"Properties": {
"@diffgr:id": "Properties1",
"@msdata:rowOrder": "0",
"@stepped": "0"
}
}
}
}
}
}
看来你的schema
有问题,请用这个schema
:
{
"properties": {
"body": {
"properties": {
"?xml": {
"properties": {
"@@encoding": {
"type": "string"
},
"@@version": {
"type": "string"
}
},
"type": "object"
},
"DataSet": {
"properties": {
"diffgr:diffgram": {
"properties": {
"@@xmlns:diffgr": {
"type": "string"
},
"@@xmlns:msdata": {
"type": "string"
},
"Results": {
"properties": {
"Properties": {
"properties": {
"@@diffgr:id": {
"type": "string"
},
"@@msdata:rowOrder": {
"type": "string"
},
"@@stepped": {
"type": "string"
}
},
"type": "object"
},
"Values": {
"items": {
"properties": {
"@@diffgr:id": {
"type": "string"
},
"@@msdata:rowOrder": {
"type": "string"
},
"@@status": {
"type": "string"
},
"@@ts": {
"type": "string"
},
"@@value": {
"type": "string"
}
},
"required": [
"@@diffgr:id",
"@@msdata:rowOrder",
"@@ts",
"@@value",
"@@status"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
},
"xs:schema": {
"properties": {
"@@id": {
"type": "string"
},
"@@xmlns": {
"type": "string"
},
"@@xmlns:msdata": {
"type": "string"
},
"@@xmlns:xs": {
"type": "string"
},
"xs:element": {
"properties": {
"@@msdata:IsDataSet": {
"type": "string"
},
"@@msdata:Locale": {
"type": "string"
},
"@@name": {
"type": "string"
},
"xs:complexType": {
"properties": {
"xs:attribute": {
"items": {
"properties": {
"@@msdata:Caption": {
"type": "string"
},
"@@name": {
"type": "string"
},
"@@type": {
"type": "string"
}
},
"required": [
"@@name",
"@@msdata:Caption",
"@@type"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}