Transform XML and JSON¶
Extract values from nested payloads and reshape them for the next system, using components for common cases and C# for complex ones.
Choose an approach¶
| Need | Use |
|---|---|
| Turn XML records into delimited lines | XmlTranslator |
| Extract XML into files using an XSD | XmlParser |
| Build or read XML with field mappings | XML Encoder Decoder |
| Convert XML to JSON, or JSON to XML | JsonConverter (set Type to JSON or XML) |
| Remap delimited records | TextMapping, Translation, MultiTranslations and macros |
| Loops, lookups, conditional rules or deep nesting | Csharp component |
Flatten XML with XmlTranslator¶
XmlTranslator writes one delimited line per XML record.
| Property | Purpose | Example |
|---|---|---|
RootElementName |
The element that contains the records | results |
RecordEndElementName |
The element that ends each record | resultItem |
TagNames |
Only extract these tags (optional) | |
XMLFieldSeparator, RecordEndSeparator, AttributeSeparator |
Characters between fields, records and attributes | ~, \|, ^ |
ReturnElementNames, ReturnAttributeName, ReturnAttributeDetails |
Include element and attribute names and values | True / False |
To build or read XML field by field, use XML Encoder Decoder with mappings such as Customer.Name={Data(2)} and an XSD.
Transform with the Csharp component¶
The Csharp component runs your own C# inside the flow. Open its Classname property to use the class generator, where you set the namespace and class name, add references and imports, and write two methods:
Process(Base.MessageController mc)runs for messages moving forward.Reply(Base.MessageController mc)runs for replies; by default it callsmc.PassAllMessagesOn().
Select Save and Compile Code to check it compiles. This example reads an account number and currency from an XML message and passes on a delimited response. A failure is logged and follows the Error link.
C#
while (true)
{
string msg = mc.Count() > 0 ? (string)mc.Pop() : "";
try
{
// Read a setting from the Common Store
string target = this.parseString(mc, "{BACKEND_ENDPOINT}");
string traceId = Guid.NewGuid().ToString();
XmlDocument doc = new XmlDocument();
doc.LoadXml(msg);
string account = doc.GetElementsByTagName("nbrAccount")[0].InnerText;
string currency = doc.GetElementsByTagName("codCurrency")[0].InnerText;
this.Logging(mc, "Out", string.Format("Account {0}, trace {1}", account, traceId));
mc.Push(string.Format("R00|SUCCESS|{0}|{1}|{2}", traceId, account, currency));
}
catch (Exception ex)
{
this.Logging(mc, "Error", ex.Message);
this.RaiseError(mc, ex.Message); // follows the Error link
break;
}
if (this.ProcessMethod == "Message" || mc.Count() == 0) break;
}
this.Continue(mc); // pass the result to the next component
Scripting members used above¶
| Member | Purpose |
|---|---|
mc.Pop(), mc.Push(), mc.Count() |
Take the next message (reducing the count), add an output message, and count pending messages. In Batch mode, pop each message in turn. |
this.parseString(mc, text) |
Evaluate a macro expression |
this.Logging(mc, type, text) |
Write to the i-Con log |
this.RaiseError(mc, text) |
Fail the component and follow its Error link |
this.ProcessMethod |
Whether the component processes one message at a time (Message) or in batches |
this.Continue(mc) |
Pass results on to the next component |