abhishek karan
abhishek karan

Reputation: 13

JSON to replace element name dynamically with value

TIBCO BW 5.14 I have below input records:

<Record>
        <TESTID>1</TESTID>
        <SCHEDULEDAY>2023-08-22</SCHEDULEDAY>
        <CUSTID>5</CUSTID>
        <MOBILENUMBER>222222222</MOBILENUMBER>
        <RADIONUM>12345</RADIONUM>
    </Record>
    <Record>
        <TESTID>2</TESTID>
        <SCHEDULEDAY>2023-08-22</SCHEDULEDAY>
        <CUSTID>5</CUSTID>
        <MOBILENUMBER>222222222</MOBILENUMBER>
        <RADIONUM>12345</RADIONUM>
    </Record>
    <Record>
        <TESTID>3</TESTID>
        <SCHEDULEDAY>2023-08-26</SCHEDULEDAY>
        <CUSTID>6</CUSTID>
        <MOBILENUMBER>222222222</MOBILENUMBER>
        <RADIONUM>12345</RADIONUM>
    </Record>

I want to use SCHEDULEDAY from above request and create JSON like this, where SCHEDULEDAY is used as element tag and both records of same SCHEDULEDAY are combined in jSON output.

Output JSON:

{
      "finalData": {
     "2023-08-22": [
        {
            "TESTID": 1,
            "CUSTID": 5,
            "Mobile": 1232434345,
            "radioNum": 11
        },
        {
            "TESTID": 3,
            "CUSTID": 5,
            "Mobile": 4545454524,
            "radioNum": 22
        }
    ],
     "2023-08-26": [
        {
            "TESTID": 5,
            "CUSTID": 6,
            "Mobile": 4543466759,
            "radioNum": 33
        }
    ]
    }
}

enter image description here

I tried iterating it over the SCHEDULDAY in TIBCO BW and then used parse json to convert the XML to JSON but its not returning the data as not sure how it can be achieved.

Upvotes: 1

Views: 74

Answers (1)

EmmanuelM
EmmanuelM

Reputation: 422

To handle this scenario you need to do the following :

. Create an XML schema that would match the structure of the target JSON document . Map the source schema to the target schema using the 'For Each / Group-by' option

Use of the group-by option is explained in BusinessWorks Process Design Guide, in chapter 8 'Mapping and Transformation' and in section Examples of Mappings (See 'Converting a List Into a Grouped List').

. Finally convert the XML to JSON with the XML to JSON activity

Upvotes: 1

Related Questions