有没有办法将 XML 输入中的内容"copy and paste"到 DataWeave 中的 JSON 输出?



我遇到了一个奇怪的情况,需要帮助:我有一个XML文档,我希望使用DW将其转换为JSON,这很容易,但我也希望使用一些属性并粘贴原始XML内容的某些部分作为其值,以避免工作管道中的接口问题。

更具体地说,我希望获取符合该类型元素列表的特定标记的所有元素,并以字符串的形式在XML中返回相同的列表。一个示例场景如下:

输入

<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>

输出

{"book": "<book category="cooking"><title lang="en">EverydayItalian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book><book category="children"><title lang="en">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book>"}

这就是我得出接近解决方案的程度。CCD_ 1和CCD_。我希望收到各种Report_Entry元素,所以这仍然是一份草案。

%dw 2.0
import * from dw::core::Types
import mergeWith from dw::core::Objects
input payload xml
output application/json
var literalKeys = (namesOf(payload.Report_Data.Report_Entry) distinctBy ((item, index) -> item)) filter ((item) -> not isObjectType(typeOf(payload.Report_Data.Report_Entry[item])))
var objectKeys = (namesOf(payload.Report_Data.Report_Entry) distinctBy ((item, index) -> item)) filter ((item) -> isObjectType(typeOf(payload.Report_Data.Report_Entry[item])))
var allObjects = payload.Report_Data.Report_Entry filterObject((value, key, index) -> objectKeys reduce ((item, accumulator = false) -> (key ~= item) or accumulator))
var justTheScalars = payload.Report_Data.Report_Entry filterObject((value, key, index) -> not (objectKeys reduce ((item, accumulator = false) -> (key ~= item) or accumulator)))
var groupedAllObjects = allObjects groupBy (item, index) -> index
---
justTheScalars mergeWith (groupedAllObjects)

我到了一个关键点,我可以

  1. 确定哪些键与对象相对应,哪些键与文字相对应
  2. 从我的输入中分割所有对象和内部的所有标量
  3. 根据对象的类将它们分组在一起,并返回合并对象

但我仍然错过了可以以某种方式将输入粘贴为字符串或将其转换为字符串以尽可能避免信息丢失的部分。

请考虑到这种情况对于DW的使用是特殊的,因为我没有关于不同Report_Entry内部会出现什么的信息,这就是为什么我使用函数组合来动态获取键和值。

对不起,我有点困惑。你只是想把输入序列化成字符串?

输入:

<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>

数据编织:

%dw 2.0
output application/json
---
payload.bookstore.*book map {
"book": write({ book: $ }, 'application/xml', {writeDeclaration: false, indent: false})
}

输出:

[
{
"book": "<book><title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book>"
},
{
"book": "<book><title lang="en">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book>"
}
]

编辑:

如果你只是想要一个字符串数组:

%dw 2.0
output application/json
---
payload.bookstore.*book map write({ book: $ }, 'application/xml', {writeDeclaration: false, indent: false})

输出:

[
"<book><title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book>",
"<book><title lang="en">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book>"
]

让DataWeave知道您想把每本书都写成XML字符串更简单。

%dw 2.0
output application/json
---
payload.bookstore  mapObject {
book: write({($$): $}, "application/xml",{writeDeclaration:false})
}

输出:

{
"book": "<book category="cooking">n  <title lang="en">Everyday Italian</title>n  <author>Giada De Laurentiis</author>n  <year>2005</year>n  <price>30.00</price>n</book>",
"book": "<book category="children">n  <title lang="en">Harry Potter</title>n  <author>J K. Rowling</author>n  <year>2005</year>n  <price>29.99</price>n</book>"
}

我更新了解决方案以发出属性并避免XML声明。

最新更新