Hi
I am working on version 18.3.
I have a CDT variable called "testCDT" like:
local!testCDT: { 'type!test'( Field_Name: "Air Ticket" Field_Value: "321" ), 'type!test'( Field_Name: "Bus Ticket" Field_Value: "123ab" ), 'type!test'( Field_Name: "Plane Ticket" Field_Value: "" ) }
Now my requirement is to convert it into following JSON format:
{"Air Ticket":"321","Bus Ticket":"123ab","Plane Ticket":""}
If i do a!toJson() on the variable local!testCDT
load( local!testCDT: { 'type!test'( Field_Name: "Air Ticket" Field_Value: "321" ), 'type!test'( Field_Name: "Bus Ticket" Field_Value: "123ab" ), 'type!test'( Field_Name: "Plane Ticket" Field_Value: "" ) }, a!toJson(local!testCDT) )
the output I receive is:
"[[{"Field_Name":"Air Ticket","Field_Value":"321"}],[{"Field_Name":"Bus Ticket","Field_Value":"123ab"}],[{"Field_Name":"Plane Ticket","Field_Value":""}]]"
Any suggestions how to achieve my desired JSON format.
Thanks in advance!!
Discussion posts and replies are publicly visible
Hi Shanmukha (raghavendrar0001)
I tried your approach in the following way:
load( local!testCDT: { 'type!test'( Field_Name: "Air Ticket" Field_Value: "321" ), 'type!test'( Field_Name: "Bus Ticket" Field_Value: "123ab" ), 'type!test'( Field_Name: "Plane Ticket" Field_Value: "" ), 'type!test'( Field_Name: "Train Ticket" Field_Value: "517bu8" ) }, local!testCDT2:a!forEach( items:local!testCDT, expression: concat(index(fv!item,"Field_Name"),char(34),":",char(34),index(fv!item,"Field_Value")) ), a!toJson(a!flatten(local!testCDT2)) )
In concat() I have used char(34) as without char(34) I was getting the output as:
"Air Ticket:321"
"Bus Ticket:123ab"
"Plane Ticket:"
"Train Ticket:517bu8"
But is not in sync with my desired output, thus used char(34).
The ultimate output that i am now getting is:
"["Air Ticket\":\"321","Bus Ticket\":\"123ab","Plane Ticket\":\"","Train Ticket\":\"517bu8"]"
Can you please suggest, why amI getting back slash (\) in my ultimate output?
Please try with the below code.
I have used a fn!stripwith() which will remove the back slash.
load( local!testCDT: { 'type!test'( Field_Name: "Air Ticket", Field_Value: "321" ), 'type!test'( Field_Name: "Bus Ticket", Field_Value: "123ab" ), 'type!test'( Field_Name: "Plane Ticket", Field_Value: "" ), 'type!test'( Field_Name: "Train Ticket", Field_Value: "517bu8" ) }, local!testCDT2: a!forEach( items: local!testCDT, expression: concat( index( fv!item, "Field_Name" ), char( 34 ), ":", char( 34 ), index( fv!item, "Field_Value" ) ) ), stripwith( a!toJson( a!flatten( local!testCDT2 ) ), "\") )
Thanks
Hi All
I have followed the following approach:
load( local!testCDT: { 'type!test'( Field_Name: "Air Ticket", Field_Value: "321" ), 'type!test'( Field_Name: "Bus Ticket", Field_Value: "123ab" ), 'type!test'( Field_Name: "Plane Ticket", Field_Value: "" ), 'type!test'( Field_Name: "Train Ticket", Field_Value: "517bu8" ) }, local!testCDT2:a!forEach( items:local!testCDT, expression:char(34)&fv!item.Field_Name&char(34)&":"&char(34)&fv!item.Field_Value&char(34) ), "{"&joinarray(local!testCDT2,",")&"}" )
This returns me a Text string but it is in the exact JSON format in which my API is expecting i.e.:
{"Air Ticket":"321","Bus Ticket":"123ab","Plane Ticket":"","Train Ticket":"517bu8"}
Now in my integration, I make the rule input of Any Type where in I am passing this text string and while calling my API in the Integration, I select the Content type of Body as JSON and pass this string and my API is working fine.
Thank you Shanmukha Aditya GIll for your help!!