I am using C# syntax to transform payload body Using conditional statements to transform body, I want to transform one key of the payload body using conditional statement if possible. More explanation of the scenario is shown in code below.
I tried to implement what I am trying to achieve but I get a error. Not sure if its just my syntax, or putting the logic there would make sense or not.
If this is the payload:
{
"dependentee_name": "Steve",
"dependentee_last_name": "Rogers",
"dependentee_comment" : "This is test",
"dependentee_relationship_primary" : "Parent",
"dependentee_relationship_secondary" : "null",
"insurer_name": "Steve",
"insurer_last_name": "Rogers",
"insurer_comment" : "This is test",
"extra_info": "This is comments"
}
As shown in the payload has a key dependentee_relationship_primary which has value Parent, so the expected transformed body would be as follows:
{
"dependentee_info": {
"name": "Steve",
"last_name": "Rogers"
},
"insurer_info": {
"i_name": "Tony",
"i_last_name": "Stark"
},
"extra_info" : "This is comments",
"relationship_type" : "Parent"
}
If this is the payload:
{
"dependentee_name": "Steve",
"dependentee_last_name": "Rogers",
"dependentee_comment" : "This is test",
"dependentee_relationship_primary" : "null",
"dependentee_relationship_secondary" : "Uncle",
"insurer_name": "Steve",
"insurer_last_name": "Rogers",
"insurer_comment" : "This is test",
"extra_info": "This is comments"
}
As shown in the payload has a key dependentee_relationship_secondaru which has value Uncle, so the expected transformed body would be as follows:
{
"dependentee_info": {
"name": "Steve",
"last_name": "Rogers"
},
"insurer_info": {
"i_name": "Tony",
"i_last_name": "Stark"
},
"extra_info" : "This is comments",
"relationship_type" : "Uncle"
}
I have attempted the code so far, but I run into an error
<policies>
<inbound>
<base />
<return-response>
<set-status code="200" reason="ok" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var body = context.Request.Body.As<JObject>(true);
var transformedBody = new JObject();
transformedBody["extra_info"] = body["extra_info"];
var dependentee= new JObject();
dependetee["name"] = body["dependtee_name"];
dependentee["lastName"] = body["depdentee_last_name"];
transformedBody["dependtee_info"] = dependentee;
var insurer_info = new JObject();
dependetee["i_name"] = body["insurer_name"];
dependentee["i_last_name"] = body["insurer_last_name"];
transformedBody["insurer_info"] = insurer_info;
if (body["dependentee_relationship_primary"] !=null)
{
transformedBody["relationship_type"] = body["dependentee_relationship_primary"];
}
else if (body["dependentee_relationship_secondary"] !=null) {
transformedBody["relationship_type"] = body["adultrelationship"];
}
return transformedBody.ToString();
}</set-body>
</return-response>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>