0
votes

I have the following set as my input payload and I want to output to be a well-structured XML-file also as seen below: I have no control over the input payload. I am new to the Mule DataWeave Transformations and Mule in general. Please note these are just arbitrary values for all names and attributes. What I have tried so far is using the pluck () and map () methods provided by Mule, but with no success. Any pointers in the right direction would be greatly appreciated.

Input:

[{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}}]}}]

Output:

<?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>
2
What type of data is in the input?George
Hello @George, the input payload is an array of JSON elements that have an array as values assigned for some of them.Nilay Patel
Could you please ensure the input data above are in JSON format?George
Yes @George, the response is in minified version of JSON objects and JSON arrays, all within an outer array. Let me format it as such, so it’s easier to see.Nilay Patel
It is not valid JSON. Please ensure it is valid and nicely formatted so it is easier to understand. Also, do you have any control of the output format?aled

2 Answers

0
votes

JSON is not valid, please post the correct JSON.

If this error is coming from the source, you may need to do some python pandas data wrangling to get it to the right format. Good luck.

0
votes

The input file is definitely not JSON (it has no quotes, it uses equals instead of colons) and I also suspect it has typos (like "instructorName{type" without equals, a parenthesis instead of a curly brace and the word "instructoreName")

Saving this, let's say you need to parse it as it is and then convert to XML. For that, you can use the following script (after changing the typos):

%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)
            })}
            }})
        }
        }
    }
}