Extract variables from json how to check if element exists

Not applicable

I am interacting with a third party service and I get the following responses :

if token valid 200 response

{
 ...
 "valid_until": "2018-09-21T00:00:00Z"
 }

if token is invalid I get 200 response with :

	{       "status": false, 

		"error": "Invalid Token Format!"

	}

I have the following extract policy

<ExtractVariables   name="Extract-valid-until-date">
    <DisplayName>Extract token chorus</DisplayName>
    <Source>chorusresponse</Source>
    <JSONPayload>
        <Variable name="valid_until">
            <JSONPath>$.valid_until</JSONPath>
        </Variable>
        <Variable name="response_error">
            <JSONPath>$.error</JSONPath>
        </Variable>
    </JSONPayload>
</ExtractVariables>

this is falling with following error if no valid_until date present in the json response:

{"fault":"{\"detail\":{\"errorcode\":\"steps.extractvariables.InvalidJSONPath\"},\"faultstring\":\"Invalid JSON path $.valid_until in policy Extract-valid-until-date.\"}"}

I tried $.?valid_until $.?(valid_until) but still not working,

is there any why to avoid error if the value is not present

Many thanks

Solved Solved
0 7 5,735
1 ACCEPTED SOLUTION

Not applicable

@miloud

Hi, you can use javascript policy.And in that you can use sth like

 var reqPayload = JSON.parse(context.getVariable('request.content'));
 var flag=reqPayload.hasOwnProperty('name');
 print(flag);

and use the flag accordingly. hasOwnProperty() will check if the property 'name' exists or not in payload. If not then will return false otherwise true.

View solution in original post

7 REPLIES 7

Not applicable

@miloud

Hi, you can use javascript policy.And in that you can use sth like

 var reqPayload = JSON.parse(context.getVariable('request.content'));
 var flag=reqPayload.hasOwnProperty('name');
 print(flag);

and use the flag accordingly. hasOwnProperty() will check if the property 'name' exists or not in payload. If not then will return false otherwise true.

so there is no solution in the extract policy, I will remove the ExtractVariables policy and replace it with javascript.

Thanks for your answers.

@miloud , Ideally Extract Variable policy should work. JSON extraction is performed only when message's Content-Type is application/json. Are you sure response contains Content-Type "application/json" ?

Hi @miloud,

You can set continueOnError="true" in your Extract-valid-until-date policy. The extract error will not be raised. In the subsequent policy step definition, you can add a condtion to validate valid_until for NULL or whatever value you need & take appropriate action.

You can also achieve the same by using JS callout which @Vipul Agarwal has posted. I dont see much different either way except you can avoid a JS callout

Thanks,

Abhishek

jivanpatil
Participant III

I know this is late for this is for anyone who faces issue. Use the extract policy mentioned in the question itself. Just add following tag in that policy.

<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> 

If you use this tag, it will ignore it if it's not able to find that key in JSON. This is useful when we receive 200 status and error in the body and you want to convert 200 to actual error with 4xx/5xx status code.

jivanpatil
Participant III

Add following tag.

<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>