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

Oracle Service Bus中XML空数组转JSON生成null的问题及修正需求

OSB中XML空数组转JSON时[null]转[]的解决方法

方案1:调整NXSD Schema配置

在定义XML结构的NXSD Schema中,给数组元素设置minOccurs="0"和maxOccurs="unbounded",同时关闭nillable="true"。比如把原来的:

<xs:element name="data" type="xs:string" nillable="true" maxOccurs="unbounded"/>

改成:

<xs:element name="data" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

这样NXSD会把空的数组节点识别为[]而非[null]。

方案2:XSLT预处理XML

在OSB管道的NXSD转换步骤前,添加XSLT转换,移除空的数组节点。示例XSLT(替换成你的命名空间):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:in="你的命名空间URI">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <!-- 匹配无内容的数组节点,不输出 -->
  <xsl:template match="in:ARRAYNAME[not(node())]"/>
</xsl:stylesheet>

处理后的XML再经过NXSD转换,就能得到空数组[]。

方案3:Java Callout后处理JSON

写一个简单的Java类,处理转换后的JSON字符串,把所有[null]替换为[]:

import org.json.JSONObject;
import org.json.JSONArray;

public class JsonArrayCleaner {
    public static String cleanNullArrays(String jsonStr) {
        JSONObject jsonObj = new JSONObject(jsonStr);
        for (String key : jsonObj.keySet()) {
            Object value = jsonObj.get(key);
            if (value instanceof JSONArray) {
                JSONArray arr = (JSONArray) value;
                if (arr.length() == 1 && arr.isNull(0)) {
                    jsonObj.put(key, new JSONArray());
                }
            }
        }
        return jsonObj.toString();
    }
}

把这个类打包成JAR部署到OSB服务器,在管道中添加Java Callout步骤,传入转换后的JSON,得到处理后的结果。

方案4:XQuery修改JSON对象

在OSB中用XQuery遍历JSON,替换值为[null]的数组:

declare namespace json = "http://xmlns.oracle.com/adf/schema/json";
declare function local:clean-arrays($json as element(json:object)) as element(json:object) {
  element json:object {
    for $entry in $json/json:entry
    return
      if ($entry/json:array[json:null]) then
        element json:entry {
          attribute key { $entry/@key },
          element json:array { }
        }
      else
        $entry
  }
};
local:clean-arrays($your-json-object)

内容的提问来源于stack exchange,提问作者M Fahmi Fachrozi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 10:45:48