1

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_secondary 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>
Markus Meyer
  • 3,327
  • 10
  • 22
  • 35
jkhan_prog
  • 43
  • 2

1 Answers1

0

First of all, it's a good idea to fix the long-existing typo issues like dependetee which I fixed for you already in:

Azure API Management (Policies)

You have to check if

  • the body has a child dependentee_relationship_primary
  • the property dependentee_relationship_primary is not null
  • the property dependentee_relationship_primary is not an empty string
if (body.ContainsKey("dependentee_relationship_primary") && body["dependentee_relationship_primary"].Type != JTokenType.Null && !string.IsNullOrEmpty(body["dependentee_relationship_primary"].Value<string>()))
{
   transformedBody["relationship_type"] = body["dependentee_relationship_primary"];
} 

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["extra_info"] = body["extra_info"];
                
                var dependentee= new JObject();
                dependentee["name"] = body["dependtee_name"];
                dependentee["lastName"] = body["depdentee_last_name"];
                transformedBody["dependtee_info"] = dependentee;

                var insurer_info = new JObject();
                insurer_info["i_name"] = body["insurer_name"];
                insurer_info["i_last_name"] = body["insurer_last_name"];
                transformedBody["insurer_info"] = insurer_info;

                if (body.ContainsKey("dependentee_relationship_primary") && body["dependentee_relationship_primary"].Type != JTokenType.Null && !string.IsNullOrEmpty(body["dependentee_relationship_primary"].Value<string>()))
                {
                    transformedBody["relationship_type"] = body["dependentee_relationship_primary"];
                } 
                else if (body.ContainsKey("dependentee_relationship_secondary") && body["dependentee_relationship_secondary"].Type != JTokenType.Null && !string.IsNullOrEmpty(body["dependentee_relationship_secondary"].Value<string>()))
                {
                    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>

Result:

enter image description here

BTW:
Your condition does not make sense: There's no adultrelationship defined.

Markus Meyer
  • 3,327
  • 10
  • 22
  • 35