Home > Blockchain >  Combining Multiple Value Json Payload using Azure APIM policies
Combining Multiple Value Json Payload using Azure APIM policies

Time:09-09

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.

CodePudding user response:

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.

CodePudding user response:

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>
  • Related