Assign Message policy- avoid inserting null values

Not applicable

In my Assign message policy I have a JSON for example like this

<Payload contentType="application/json">{a:"A","b":"{example.B}"}</Payload>
      

if {example.B} is null or undefined,

is there a way not having b in the JSON?
i.e.:

{a:"A"}
Solved Solved
0 1 767
1 ACCEPTED SOLUTION

@YuriAbaev

I don't think you can implement any programming logic related to policy itself inside a particular policy.But, You can achieve the same using Javascript Policy / Using policy conditions.

For example, Using Javascript,

var payload = {a: "A"};
if (context.getVariable("example.B") != undefined) {
	payload.b = context.getVariable("example.B");	
}
context.setVariable("customPayload", JSON.stringify(payload));

Using Policy Conditions, you need two Assign Message Policies,

Policy 1 , If b is not undefined, Execute below policy using policy condition where you check for value of example.B,

<Payload contentType="application/json">{a:"A","b":"{example.B}"}</Payload>

Proxy 2, If b is undefined, Execute below policy using policy condition where you check for value of example.B,

<Payload contentType="application/json">{a:"A"}</Payload>

Hope it helps.

View solution in original post

1 REPLY 1

@YuriAbaev

I don't think you can implement any programming logic related to policy itself inside a particular policy.But, You can achieve the same using Javascript Policy / Using policy conditions.

For example, Using Javascript,

var payload = {a: "A"};
if (context.getVariable("example.B") != undefined) {
	payload.b = context.getVariable("example.B");	
}
context.setVariable("customPayload", JSON.stringify(payload));

Using Policy Conditions, you need two Assign Message Policies,

Policy 1 , If b is not undefined, Execute below policy using policy condition where you check for value of example.B,

<Payload contentType="application/json">{a:"A","b":"{example.B}"}</Payload>

Proxy 2, If b is undefined, Execute below policy using policy condition where you check for value of example.B,

<Payload contentType="application/json">{a:"A"}</Payload>

Hope it helps.