0

I need to transform a set of incoming jsons as below to a common structure at the output. The structure of the incoming jsons are as below

Input JSON 1 { "JR_ID": "1", "JR_Data": "some text" }

Input JSON 2 { "TA_ID": "1", "TA_Data": "some text" }

Input JSON 3

{ "IM_ID": "1", "IM_Data": "some text" }

..and many more

The output JSON structure is as below

Output structure

{ "OBJECT_ID": "1", "OBJECT_Data": "some text" }

The input jsons will come one at a time and the task is to dynamically map the ID field of the incoming JSON to OBJECT_ID of output JSON and same for the data field.

How can I use mule expression language or dataweave for this? Is there any other solution available?

Any help is much appreciated.

user1356042
  • 395
  • 2
  • 6
  • 23

2 Answers2

1

Given that your input seems to map to your output via *_ID or *_Data, this script should help you out:

%dw 1.0
%output application/json
---
payload pluck ($$ as :string) reduce ((key, out={}) ->
  key match {
    k when key contains "ID"   -> out ++ {"OBJECT_ID":   payload[k]},
    k when key contains "Data" -> out ++ {"OBJECT_Data": payload[k]},
    default                    -> out  
  }
)

What this is doing is first getting a list of all the keys in the input object -- payload pluck ($$ as :string). The cast as :string is so that we can later test if the key contains a certain substring. Then we use reduce to build the output object you're looking for, by adding an OBJECT_ID key:value pair to the output object when the input key contains "ID" and adding an OBJECT_Data key:value pair to the output object when the key contains "Data".

jerney
  • 2,187
  • 1
  • 19
  • 31
0

You can dynamically map the JSON input using following dataweave syntax:

%dw 1.0
%output application/json
---
{
    OBJECT_ID : payload[0],
    OBJECT_Data : payload[1]
}
Vatsal Mehta
  • 117
  • 2
  • 19