骡子 4:将 SQL XML 对象转换为 JSON
Mule 4: Transforming SQL XML object to JSON
我得到 SQLXML 结果集作为从数据库执行存储过程形状调用的 MS SQL 存储过程的输出。
SQLXML 结果集如下所示:
<xmlcontent>
<field1>val1</field1>
<field2>val2</field2>
<field3>val3</filed3>
</xmlcontent>
<xmlcontent>
<field1>val1</field1>
<field2>val2</field2>
<field3>val3</filed3>
</xmlcontent>
<xmlcontent>
<field1>val1</field1>
<field2>val2</field2>
<field3>val3</filed3>
</xmlcontent>
然而,payload 有点奇怪,因为它包含长度为 2033 的 XML 输出,然后作为数组元素重复,直到 payload 耗尽。带有 just 有效载荷的变换形状输出显示:
{
"resultSet1": [
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "<xmlcontent><field1/><field2/><field3/>" --this content is of size 2033 max
},
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "</xmlcontent><xmlcontent><field1/><fiel" --this content is of size 2033 max
},
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "d2/><field3/><xmlcontent>"
}
]
}
我正在考虑使用另一个转换形状从 resultset1 数组中附加“XML_F52E2B61-18A1-11d1-B105-00805F49916B”的值,然后转换为 JSON。数据编织中有没有一种方法可以简化这种方法以获得如下所示的最终 JSON 输出?
[
{
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
{
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
{
"field1": "val1",
"field2": "val2",
"field3": "val3"
}
]
输入:
{
"resultSet1": [
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "<xmlcontent><field1>val1</field1><field2>val2</field2><field3>val3</field3>"
},
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "</xmlcontent><xmlcontent><field1>val1</field1><fiel"
},
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "d2>val2</field2><field3>val3</field3></xmlcontent>"
}
]
}
脚本:
%dw 2.0
output application/json
var id='XML_F52E2B61-18A1-11d1-B105-00805F49916B'
var body=payload.resultSet1 map (
$[id]) reduce ((item, accumulator) -> accumulator ++ item)
---
read('<root>' ++ body ++ '</root>', "application/xml").root.*xmlcontent
输出:
[
{
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
{
"field1": "val1",
"field2": "val2",
"field3": "val3"
}
]
我得到 SQLXML 结果集作为从数据库执行存储过程形状调用的 MS SQL 存储过程的输出。
SQLXML 结果集如下所示:
<xmlcontent>
<field1>val1</field1>
<field2>val2</field2>
<field3>val3</filed3>
</xmlcontent>
<xmlcontent>
<field1>val1</field1>
<field2>val2</field2>
<field3>val3</filed3>
</xmlcontent>
<xmlcontent>
<field1>val1</field1>
<field2>val2</field2>
<field3>val3</filed3>
</xmlcontent>
然而,payload 有点奇怪,因为它包含长度为 2033 的 XML 输出,然后作为数组元素重复,直到 payload 耗尽。带有 just 有效载荷的变换形状输出显示:
{
"resultSet1": [
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "<xmlcontent><field1/><field2/><field3/>" --this content is of size 2033 max
},
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "</xmlcontent><xmlcontent><field1/><fiel" --this content is of size 2033 max
},
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "d2/><field3/><xmlcontent>"
}
]
}
我正在考虑使用另一个转换形状从 resultset1 数组中附加“XML_F52E2B61-18A1-11d1-B105-00805F49916B”的值,然后转换为 JSON。数据编织中有没有一种方法可以简化这种方法以获得如下所示的最终 JSON 输出?
[
{
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
{
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
{
"field1": "val1",
"field2": "val2",
"field3": "val3"
}
]
输入:
{
"resultSet1": [
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "<xmlcontent><field1>val1</field1><field2>val2</field2><field3>val3</field3>"
},
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "</xmlcontent><xmlcontent><field1>val1</field1><fiel"
},
{
"XML_F52E2B61-18A1-11d1-B105-00805F49916B": "d2>val2</field2><field3>val3</field3></xmlcontent>"
}
]
}
脚本:
%dw 2.0
output application/json
var id='XML_F52E2B61-18A1-11d1-B105-00805F49916B'
var body=payload.resultSet1 map (
$[id]) reduce ((item, accumulator) -> accumulator ++ item)
---
read('<root>' ++ body ++ '</root>', "application/xml").root.*xmlcontent
输出:
[
{
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
{
"field1": "val1",
"field2": "val2",
"field3": "val3"
}
]