【问题标题】:DataWeave Transformation in MuleMule 中的 DataWeave 转换
【发布时间】:2020-04-13 08:06:20
【问题描述】:

我将以下设置作为我的输入有效负载,并且我希望输出为结构良好的 XML 文件,如下所示:我无法控制输入有效负载。总的来说,我是 Mule DataWeave Transformations 和 Mule 的新手。请注意,这些只是所有名称和属性的任意值。到目前为止,我尝试的是使用 Mule 提供的 pluck() 和 map() 方法,但没有成功。任何指向正确方向的指针将不胜感激。

输入:

[{head={vars=[subject, instructorCode, instructoreName]}, results={bindings=[{subject={type=uri, value=www.google.com/subject-1-Details}, instructorName={type=literal, value=John Smith}, instructorCode={type=literal, value=JOS}}, {subject={type=uri, value=www.google.com/subject-2-Details}, instructorName{type=literal, value=Jane Smith}, instructorCode={type=literal, value=JAS}}]}},{head={vars=[department, departmentCode, departmentName]}, results={bindings=[{department={type=uri, value=www.google.com/department-1-details}, departmentName={type=literal, value=Computer Science}, departmentCode={type=literal, value=CS}},{department=(type=uri, value=www.google.com/department-2-details}, departmentName={type=literal, value = English}, departmentCode={type=literal, value=EL}}]}}]

输出:

<?xml version="1.0" encoding="UTF-8"?>
<CustomMessage xmlns="knowledge.publish.google.com">
  <CustomKLMessage>
    <CustomMessageHeader>
      <msgId>12353</msgId>
      <requestDateTime<2019-12-20T16:04:19.099-05:00</requestDateTime>
      <requestorID>XYZ123</requestorID>
      <locale>en_US</locale>
    </CustomMessageHeader>
    <elements>
      <element>
        <name>subject</name>
        <values>
          <value>JOS||John Smith</value>
          <value>JAS||Jane Smith</value>
          <value>NIP||Nilay Patel</value>
        </values>
      </element>
      <element>
        <name>department</name>
        <values>
          <value>CS||Computer Science</value>
          <value>EL||English Language</value>
        </values>
      </element>
    </elements>
  </CustomKLMessage>
</CustomMessage>

【问题讨论】:

  • 输入的数据是什么类型的?
  • 你好@George,输入有效负载是一个 JSON 元素数组,其中有一个数组作为分配给其中一些元素的值。
  • 请确保以上输入数据为JSON格式?
  • 是的@George,响应是 JSON 对象和 JSON 数组的缩小版本,都在一个外部数组中。让我这样格式化,这样更容易看到。
  • 它不是有效的 JSON。请确保它有效且格式正确,以便于理解。另外,您对输出格式有任何控制权吗?

标签: xml mule mule-studio mule-component dataweave


【解决方案1】:

输入文件绝对不是 JSON(它没有引号,它使用等号而不是冒号)而且我也怀疑它有拼写错误(比如没有等号的“instructorName{type”,括号而不是花括号和单词"教员姓名")

保存它,假设您需要按原样解析它,然后转换为 XML。为此,您可以使用以下脚本(更改拼写错误后):

%dw 2.0
output application/xml
// Convert to JSON which is the most similar format
var jsonText = 
    (
        // replace equals with colons 
        (payload replace "=" with ":") 
        // remove spaces between special chars
        replace /([\[\]\{\},:]+)\s+([\[\]\{\},:]+)/ 
            with  (mat,index) -> mat[1] ++ mat[2]
    )
    // wrap values with quotes
    replace /([^\[\]\{\},\=:]+)/ 
        with (mat,index) -> "\"" ++ trim(mat[1]) ++ "\""
// Parse it as JSON
var json = read(jsonText,"application/json")
ns ns0 knowledge.publish.google.com
---
{ ns0#CustomMessage: {
        ns0#CustomKLMessage: {
        ns0#CustomMessageHeader: {
            ns0#msgId: "12353",
            ns0#requestDateTime: "2019-12-20T16:04:19.099-05:00",
            ns0#requestorID: "XYZ123",
            ns0#locale: "en_US"
        },
        ns0#elements: {(json map (item,idx) -> {
            ns0#element: {
            ns0#name: item.head.vars[0],
            ns0#values: {( item.results.bindings map (bind,idx2) ->  {
                ns0#value: bind[(item.head.vars[1])].value ++ "||" ++ (bind[(item.head.vars[2])].value)
            })}
            }})
        }
        }
    }
}

【讨论】:

    【解决方案2】:

    JSON 无效,请发布正确的 JSON。

    如果此错误来自源,您可能需要进行一些 python pandas 数据整理以将其转换为正确的格式。祝你好运。

    【讨论】:

    • 你好@Siva,输入的类型是 SPARQL + JSON,我猜它是在扩展名为 .srj 的文件中发送的。此外,我们将所有数据解析为一个数组。所以输入仍然是 SPARQL + JSON,但在一个数组中。使用正确的格式相应地更新输入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多