You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSO2 Data Service查询报错:结果记录中无法包含JSON数组

解决DataService返回JSON配置中"A JSON Array cannot be contained in the result records"错误

问题现象

调用DataService执行SQL查询时触发错误:

  • 服务标识:studenttest-1.0.0.dbs
  • 错误信息:A JSON Array cannot be contained in the result records
  • 错误码:UNKNOWN_ERROR

对应的DataService配置如下:

<sql>SELECT * from etudiant where nom =?</sql>
<param name="nom" sqlType="STRING" />
<result outputType="json">
{
  "Envelope": {
    "Body": {
      "GetFacturesClientResponse": {
        "GetFacturesClientResult": {
          "code": 0,
          "nbreFactures": 2,
          "totalResteAPayerFactures": 67.832,
          "listeFactures": {
            "Facture": [
              {
                "id":"$id",
                "nom":"$nom",
                "prenom":"$prenom",
                "age":"$age",
                "note":"$note"
              }
            ]
          },
          "codeClient": "P-2008-043681",
          "nom": "Oussama",
          "prenom": "Haythem"
        }
      }
    }
  }
}
</result>
</query>

错误原因

这个错误的核心是DataService在映射SQL结果到JSON结构时,无法处理非数组容器下直接嵌入数组的逻辑冲突。你的SQL查询SELECT * from etudiant where nom =?会返回多条etudiant记录,框架默认会把多行结果转为数组,但当前JSON模板里,Facture数组嵌套在固定结构中,模板没有明确告知框架如何将多行SQL结果对应到数组元素,导致映射逻辑混乱。

解决方案

1. 调整JSON模板的映射逻辑

修改模板,让框架明确知道将每一行SQL结果填充到Facture数组的对象中,同时可用内置变量动态获取记录数等信息:

<sql>SELECT * from etudiant where nom =?</sql>
<param name="nom" sqlType="STRING" />
<result outputType="json">
{
  "Envelope": {
    "Body": {
      "GetFacturesClientResponse": {
        "GetFacturesClientResult": {
          "code": 0,
          "nbreFactures": "$rowCount", <!-- 用$rowCount自动获取SQL返回的记录总数 -->
          "totalResteAPayerFactures": 67.832, <!-- 如需动态计算总金额,可在SQL中添加聚合函数并映射 -->
          "listeFactures": {
            "Facture": [
              {
                "id":"$id",
                "nom":"$nom",
                "prenom":"$prenom",
                "age":"$age",
                "note":"$note"
              }
            ]
          },
          "codeClient": "P-2008-043681",
          "nom": "$nom", <!-- 如需取第一条记录的姓名则保留,否则设为固定值 -->
          "prenom": "$prenom"
        }
      }
    }
  }
}
</result>
</query>

2. 验证框架映射规则

确认你使用的DataService框架是否要求数组必须添加特定标识(比如repeatable="true")来指明该节点需要循环填充SQL多行结果,可参考框架文档补充配置。

3. 分场景测试

  • 先测试SQL返回单条记录的情况,确认模板能正常生成JSON结构;
  • 再测试返回多条记录的场景,检查Facture数组是否正确包含所有查询结果。

内容的提问来源于stack exchange,提问作者Oussama Nairi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 21:35:16