Reputation: 59
Hi I have a current payload in APIM, which I want to transform into another payload.
Current Payload:
{
"insurance_id": "2112",
"insurer_info": {
"first": "Tony",
"last": "Stark"
}
}
Expected Payload
{
"id": "2112",
"insurer_name": {
"fullname": "Tony Stark"
}
}
Attempt of the code:
<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["id"] = body["insurance_id"];
var insurerName= new JObject();
dependentee["fullname"] = body["insurer_info"]["first"]["last"];
transformedBody["insurerName"] = insurerName;
return transformedBody.ToString();
}</set-body>
</return-response>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
What I am trying to achieve is that I want to extract first and last name from the current payload. And show as full name as shown in the expected payload. What I have implemented above is wrong, and I dont understand how I can go about combining values.
Upvotes: 2
Views: 571
Reputation:
You could use C# for this(Little bit harder than the next but got a lot of features) or use Liquid template(Easy to work with JSON)
Liquid( This template does not work in <return-response> )
DOC: https://shopify.github.io/liquid/tags/template/
<policies>
<inbound>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
<set-body template="liquid">
{
"insurance_id": "2112",
"insurer_info": {
"first": "Tony",
"last": "Stark"
}
}
</set-body>
<set-body template="liquid">
{
"id": "{{ body.insurance_id }}",
"insurer_name": {
"fullname": "{{ body.insurer_info.first }} {{ body.insurer_info.last }}"
}
}
</set-body>
</on-error>
</policies>
C#
<policies>
<inbound>
<base />
<set-body template="liquid">
{
"insurance_id": "2112",
"insurer_info": {
"first": "Tony",
"last": "Stark"
}
}
</set-body>
<return-response>
<set-status code="200" reason="OK" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body template="none">@{
JObject body = context.Request.Body.As<JObject>(preserveContent: false);
JObject response = new JObject {
["id"] = body["insurance_id"],
["insurer_name"] = new JObject {
["fullname"] = $"{body["insurer_info"]["first"]} {body["insurer_info"]["last"]}"
}
};
return response.ToString();
}</set-body>
</return-response>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
Upvotes: 0
Reputation: 227
You can also use the "set body liquid template" like this:
<set-body template="liquid">
{
"id": "{{body.insurance_id}}",
"insurer_name": {
"fullname": "{{body.insurer_info.first}} {{body.insurer_info.last}}"
}
}
</set-body>
Upvotes: 0
Reputation: 3937
You have to read the JObject:
var insurerInfo = body["insurer_info"] as JObject;
You are getting the fullname with string concatination:
var fullName = insurerInfo["first"]?.Value<string>() + " " + insurerInfo["last"]?.Value<string>();
The complete policy:
<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["id"] = body["insurance_id"];
var insurerInfo = body["insurer_info"] as JObject;
if(insurerInfo != null)
{
var fullName = insurerInfo["first"]?.Value<string>() + " " + insurerInfo["last"]?.Value<string>();
var insurerName = new JObject();
insurerName["fullname"] = fullName;
transformedBody["insurerName"] = insurerName;
}
return transformedBody.ToString();
}</set-body>
</return-response>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
BTW: Please take care about your copied code from other questions: dependentee
does not exist here.
Upvotes: 1