Azure Logic Apps Standard解析WHMCS API返回JSON报错问题
Logic Apps Parse JSON处理WHMCS API返回数据失败问题
我在Logic Apps中使用Parse JSON卡片处理简单JSON时完全正常,比如:
示例JSON:
{ "Hello":"World" }
对应的Schema:
{ "type": "object", "properties": { "Hello": { "type": "string" } } }
但调用WHMCS GetClientsProducts API后,用Parse JSON解析返回数据时却报错,哪怕Schema是直接从之前HTTP请求的Payload自动生成的。使用的Schema如下:
{ "type": "object", "properties": { "result": { "type": "string" }, "clientid": { "type": "string" }, "serviceid": { "type": "string" }, "pid": { "type": "string" }, "domain": { "type": "string" }, "totalresults": { "type": "integer" }, "startnumber": { "type": "integer" }, "numreturned": { "type": "integer" }, "products": { "type": "object", "properties": { "product": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "integer" }, "clientid": { "type": "integer" }, "orderid": { "type": "integer" }, "ordernumber": { "type": "integer" }, "pid": { "type": "integer" }, "regdate": { "type": "string" }, "name": { "type": "string" }, "translated_name": { "type": "string" }, "groupname": { "type": "string" }, "translated_groupname": { "type": "string" }, "domain": { "type": "string" }, "dedicatedip": { "type": "string" }, "serverid": { "type": "integer" }, "servername": { "type": "string" }, "serverip": {}, "serverhostname": {}, "suspensionreason": { "type": "string" }, "firstpaymentamount": { "type": "string" }, "recurringamount": { "type": "string" }, "paymentmethod": { "type": "string" }, "paymentmethodname": { "type": "string" }, "billingcycle": { "type": "string" }, "nextduedate": { "type": "string" }, "status": { "type": "string" }, "username": { "type": "string" }, "password": { "type": "string" }, "subscriptionid": { "type": "string" }, "promoid": { "type": "integer" }, "overideautosuspend": { "type": "integer" }, "overidesuspenduntil": { "type": "string" }, "ns1": { "type": "string" }, "ns2": { "type": "string" }, "assignedips": { "type": "string" }, "notes": { "type": "string" }, "diskusage": { "type": "integer" }, "disklimit": { "type": "integer" }, "bwusage": { "type": "integer" }, "bwlimit": { "type": "integer" }, "lastupdate": { "type": "string" }, "customfields": { "type": "object", "properties": { "customfield": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" }, "translated_name": { "type": "string" }, "value": { "type": "string" } } } } } }, "configoptions": { "type": "object", "properties": { "configoption": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "integer" }, "option": { "type": "string" }, "type": { "type": "string" }, "value": { "type": "string" } } } } } } } } } } } } }
问题排查与解决方案
1. 修正空类型字段定义
你的Schema中serverip和serverhostname字段的定义是{},没有指定具体类型,这会导致Logic Apps的Parse JSON无法正确识别字段类型。根据WHMCS API的实际返回,这两个字段通常是字符串或null,所以可以修改为:
"serverip": { "type": ["string", "null"] }, "serverhostname": { "type": ["string", "null"] }
2. 处理单条目数组的歧义问题
WHMCS API在返回单个产品时,可能会将product字段从数组转为单个对象,而你的Schema定义为array类型,这会直接导致解析失败。可以修改Schema兼容数组和对象两种情况:
"product": { "type": ["array", "object"], "items": { // 保持原有的item定义 } }
之后在Logic Apps的后续步骤中,可以用if()函数判断类型,比如:
if(isArray(products.product), products.product, createArray(products.product))
3. 允许字段为null
部分字段(如promoid、suspensionreason)可能返回null,但你的Schema只定义了基础类型,需要添加null允许:
"promoid": { "type": ["integer", "null"] }, "suspensionreason": { "type": ["string", "null"] }
4. 基于完整Payload重新生成Schema
自动生成的Schema可能基于不完整的测试数据(比如测试时某些字段未返回),导致类型定义缺失或错误。建议调用API获取包含所有可能字段的完整Payload,再重新生成Schema,之后手动检查修正上述的类型问题。
内容的提问来源于stack exchange,提问作者Shirofuji
相关产品推荐
相关产品推荐

