Logic Apps工作流API响应处理(二):Parse JSON Schema设置
在Logic Apps中适配两种API响应的Parse JSON Schema配置方案
由于第三方API返回的响应可能是数组格式的成功结果或对象格式的错误信息,你需要使用JSON Schema的oneOf关键字来定义可兼容两种类型的Schema,具体步骤如下:
1. 生成两种响应的基础Schema
首先分别将成功和错误响应的示例JSON转换为Schema:
错误响应的Schema
{ "type": "object", "properties": { "Number": { "type": "integer" }, "Description": { "type": "string" }, "Cause": { "type": "string" }, "Resolution": { "type": "string" } }, "required": [ "Number", "Description", "Cause", "Resolution" ] }
成功响应的Schema
{ "type": "array", "items": { "type": "object", "properties": { "Input": { "type": "object", "properties": { "Address": { "type": "string" }, "Address1": { "type": "string" }, "Address2": { "type": "string" }, "Address3": { "type": "string" }, "Address4": { "type": "string" }, "Address5": { "type": "string" }, "Address6": { "type": "string" }, "Address7": { "type": "string" }, "Address8": { "type": "string" }, "Country": { "type": "string" }, "SuperAdministrativeArea": { "type": "string" }, "AdministrativeArea": { "type": "string" }, "SubAdministrativeArea": { "type": "string" }, "Locality": { "type": "string" }, "DependentLocality": { "type": "string" }, "DoubleDependentLocality": { "type": "string" }, "Thoroughfare": { "type": "string" }, "DependentThoroughfare": { "type": "string" }, "Building": { "type": "string" }, "Premise": { "type": "string" }, "SubBuilding": { "type": "string" }, "PostalCode": { "type": "string" }, "Organization": { "type": "string" }, "PostBox": { "type": "string" } }, "required": [ "Address", "Address1", "Address2", "Address3", "Address4", "Address5", "Address6", "Address7", "Address8", "Country", "SuperAdministrativeArea", "AdministrativeArea", "SubAdministrativeArea", "Locality", "DependentLocality", "DoubleDependentLocality", "Thoroughfare", "DependentThoroughfare", "Building", "Premise", "SubBuilding", "PostalCode", "Organization", "PostBox" ] }, "Matches": { "type": "array", "items": { "type": "object", "properties": { "AQI": { "type": "string" }, "AVC": { "type": "string" }, "Address": { "type": "string" }, "Address1": { "type": "string" }, "Address2": { "type": "string" }, "AdministrativeArea": { "type": "string" }, "CountryName": { "type": "string" }, "DeliveryAddress": { "type": "string" }, "DeliveryAddress1": { "type": "string" }, "GeoAccuracy": { "type": "string" }, "GeoDistance": { "type": "string" }, "HyphenClass": { "type": "string" }, "ISO3166-2": { "type": "string" }, "Country": { "type": "string" }, "ISO3166-3": { "type": "string" }, "ISO3166-N": { "type": "string" }, "Latitude": { "type": "string" }, "Locality": { "type": "string" }, "Longitude": { "type": "string" }, "MatchRuleLabel": { "type": "string" }, "PostalCode": { "type": "string" }, "PostalCodePrimary": { "type": "string" }, "Premise": { "type": "string" }, "PremiseNumber": { "type": "string" }, "SubAdministrativeArea": { "type": "string" }, "Thoroughfare": { "type": "string" }, "DependentLocality": { "type": "string" } }, "required": [ "AQI", "AVC", "Address", "Address1", "Address2", "AdministrativeArea", "CountryName", "DeliveryAddress", "DeliveryAddress1", "GeoAccuracy", "GeoDistance", "HyphenClass", "ISO3166-2", "Country", "ISO3166-3", "ISO3166-N", "Latitude", "Locality", "Longitude", "MatchRuleLabel", "PostalCode", "PostalCodePrimary", "Premise", "PremiseNumber", "SubAdministrativeArea", "Thoroughfare" ] } } }, "required": ["Input", "Matches"] } }
2. 合并为兼容两种类型的复合Schema
使用oneOf关键字将两个基础Schema组合,这样Parse JSON动作就能识别两种响应类型:
{ "oneOf": [ // 错误响应Schema { "type": "object", "properties": { "Number": { "type": "integer" }, "Description": { "type": "string" }, "Cause": { "type": "string" }, "Resolution": { "type": "string" } }, "required": ["Number", "Description", "Cause", "Resolution"] }, // 成功响应Schema { "type": "array", "items": { "type": "object", "properties": { "Input": { "type": "object", "properties": { "Address": { "type": "string" }, "Address1": { "type": "string" }, "Address2": { "type": "string" }, "Address3": { "type": "string" }, "Address4": { "type": "string" }, "Address5": { "type": "string" }, "Address6": { "type": "string" }, "Address7": { "type": "string" }, "Address8": { "type": "string" }, "Country": { "type": "string" }, "SuperAdministrativeArea": { "type": "string" }, "AdministrativeArea": { "type": "string" }, "SubAdministrativeArea": { "type": "string" }, "Locality": { "type": "string" }, "DependentLocality": { "type": "string" }, "DoubleDependentLocality": { "type": "string" }, "Thoroughfare": { "type": "string" }, "DependentThoroughfare": { "type": "string" }, "Building": { "type": "string" }, "Premise": { "type": "string" }, "SubBuilding": { "type": "string" }, "PostalCode": { "type": "string" }, "Organization": { "type": "string" }, "PostBox": { "type": "string" } }, "required": [ "Address", "Address1", "Address2", "Address3", "Address4", "Address5", "Address6", "Address7", "Address8", "Country", "SuperAdministrativeArea", "AdministrativeArea", "SubAdministrativeArea", "Locality", "DependentLocality", "DoubleDependentLocality", "Thoroughfare", "DependentThoroughfare", "Building", "Premise", "SubBuilding", "PostalCode", "Organization", "PostBox" ] }, "Matches": { "type": "array", "items": { "type": "object", "properties": { "AQI": { "type": "string" }, "AVC": { "type": "string" }, "Address": { "type": "string" }, "Address1": { "type": "string" }, "Address2": { "type": "string" }, "AdministrativeArea": { "type": "string" }, "CountryName": { "type": "string" }, "DeliveryAddress": { "type": "string" }, "DeliveryAddress1": { "type": "string" }, "GeoAccuracy": { "type": "string" }, "GeoDistance": { "type": "string" }, "HyphenClass": { "type": "string" }, "ISO3166-2": { "type": "string" }, "Country": { "type": "string" }, "ISO3166-3": { "type": "string" }, "ISO3166-N": { "type": "string" }, "Latitude": { "type": "string" }, "Locality": { "type": "string" }, "Longitude": { "type": "string" }, "MatchRuleLabel": { "type": "string" }, "PostalCode": { "type": "string" }, "PostalCodePrimary": { "type": "string" }, "Premise": { "type": "string" }, "PremiseNumber": { "type": "string" }, "SubAdministrativeArea": { "type": "string" }, "Thoroughfare": { "type": "string" }, "DependentLocality": { "type": "string" } }, "required": [ "AQI", "AVC", "Address", "Address1", "Address2", "AdministrativeArea", "CountryName", "DeliveryAddress", "DeliveryAddress1", "GeoAccuracy", "GeoDistance", "HyphenClass", "ISO3166-2", "Country", "ISO3166-3", "ISO3166-N", "Latitude", "Locality", "Longitude", "MatchRuleLabel", "PostalCode", "PostalCodePrimary", "Premise", "PremiseNumber", "SubAdministrativeArea", "Thoroughfare" ] } } }, "required": ["Input", "Matches"] } } ] }
3. 在Logic Apps中配置并处理分支
- 将上述复合Schema粘贴到Parse JSON动作的Schema输入框中,内容源选择第三方API的响应输出。
- 添加Condition动作,判断响应类型:
- 条件表达式写:
contains(body('Parse_JSON'), 'Number') - 条件为真时,处理错误响应(比如记录日志、返回错误提示)
- 条件为假时,处理成功的数组响应(比如遍历数组处理每个地址匹配结果)
- 条件表达式写:
内容的提问来源于stack exchange,提问作者Francesco
相关产品推荐
相关产品推荐

