We have incoming JSON data from Source which has data and column in same payload. Using json-to-xml in XSLT I was able to generate XML like following:
<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
<boolean key="allData">true</boolean>
<map key="factMap">
<map key="T!T">
<array key="rows">
<map>
<array key="dataCells">
<map>
<string key="label">1A</string>
<string key="value">1A</string>
</map>
<map>
<string key="label">1B</string>
<string key="value">1B</string>
</map>
<map>
<string key="label">1C</string>
<string key="value">1C</string>
</map>
</array>
</map>
<map>
<array key="dataCells">
<map>
<string key="label">2A</string>
<string key="value">2A</string>
</map>
<map>
<string key="label">2B</string>
<string key="value">2B</string>
</map>
<map>
<string key="label">2C</string>
<string key="value">2C</string>
</map>
</array>
</map>
<map>
<array key="dataCells">
<map>
<string key="label">3A</string>
<string key="value">3A</string>
</map>
<map>
<string key="label">3B</string>
<string key="value">3B</string>
</map>
<map>
<string key="label">3C</string>
<string key="value">3C</string>
</map>
</array>
</map>
</array>
</map>
</map>
<map key="detailColumnInfo">
<map key="Product_vod__c.F1">
<string key="dataType">string</string>
<string key="label">F1</string>
</map>
<map key="Product_vod__c.F2">
<string key="dataType">string</string>
<string key="label">F2</string>
</map>
<map key="Product_vod__c.F3">
<string key="dataType">string</string>
<string key="label">F3</string>
</map>
</map>
</map>
Here each row is available under dataCells tag as label and value. Value under dataCells represents value of field however label does not represent field name. Field names and field order is part of detailColumnInfo tag. Order and number of fields/data in is not fixed however target XML shall remain fixed. E.g. incoming data can have 10 fields and data in any order however target XML shall have only 2 fields. To implement this, I believe, on run time we have to figure out the order of F1 field and have to pick corresponding data from dataCells. E.g. if F1 is field is first field in detailColumnInfo then I have to pick first value from all dataCells (1A, 2A, 3A)and generate target XML with value of F1 field and so on.
Detailcolumninfo tag represents the column details. Position of value field in Detailcolumninfo will determine the position of fields in Datacells. For input code snippet posted in query, if F1 is the 1st label in Detailcolumninfo then first value in Datacells (1A,2A,3A) represents the value of F1. Similarly 1B,2B,3B represents value of F2. Target XML should look like following:
<Root>
<Rows>
<Row>
<F1> 1A </F1>
<F2> 1B </F2>
<F3> 1C </F3>
</Row>
<Row>
<F1>2A </F1>
<F2> 2B </F2>
<F3> 2C </F3>
</Row>
<Row>
<F1>3A </F1>
<F2> 3B </F2>
<F3> 3C </F3>
</Row>
</Rows>
Can anyone suggest possible solutions to implement such dynamic mapping.
Here is an example that assumes you simply want to map each
array
element withkey="dataCells"
to aRow
element and then output its descendantsstring
withkey="value"
wrapped in the label from the detailColumnInfo:Output is