Extract variables from json if one of the element is not present

I have similar policy in my proxy :

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

If valid_until variable is not present in request, it throws an error :

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

if i changed continueOnError="true", then it moves ahead but it doesn't capture second element value as well because the error has come on 1st element and policy was terminated there itself. I can use JS as well for my purpose. But is there any way to handle in extract variable policy ?

Thanks

0 5 672
5 REPLIES 5

Works for me. Can you confirm the Source - "Chorusresponse" is a valid JSON and has the Content-Type as "application/json" ? Or can you check if the JSONpath you provided is correct ? Can you share your payload info ?

It doesnt fail if the other variable is null

My policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables-1">
    <DisplayName>Extract Variables-1</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="valid_until">
            <JSONPath>$.valid_until</JSONPath>
        </Variable>
        <Variable name="response_error">
            <JSONPath>$.error</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">request</Source>
    <VariablePrefix>reqPrefix</VariablePrefix>
</ExtractVariables>

My test payloads were:

{
	"valid_until": "sfgf"
}

and

{
	"error": "dfzgfdfg"
}

I was able to see the variables being populated

I tried below request from postman and it failed :

{ "id": "https://test.salesforce.com", "asserted_user": true, "user_id": "0052969023DihTAAS", "organization_id": "00D29690200ra8EAA", "username": "jai@google.com1", "nick_name": "google69020345668", "display_name": "R 5.9 Order1", "email": "jai@google.com", "email_verified": false, "first_name": "R 5.9", "last_name": "Order1", "timezone": "America/Sao_Paulo", "photos": { "picture": "google.comimg/userprofile", "thumbnail": "google.comimg/userprofile" }, "addr_street": null, "addr_city": null, "addr_state": null, "addr_country": null, "addr_zip": null, "mobile_phone": "45654", "mobile_phone_verified": false, "is_lightning_login_user": false, "status": { "created_date": null, "body": null }, "urls": { "enterprise": "google.comservices/Soap/c/{version}6902", "metadata": "google.comservices/Soap/m/{version}6902", "partner": "google.comservices/Soap/u/{version}6902", "rest": "google.comservices/data/v{version}/", "sobjects": "google.comservices/data/v{version}/sobjects/", "search": "google.comservices/data/v{version}/search/", "query": "google.comservices/data/v{version}/query/", "recent": "google.comservices/data/v{version}/recent/", "tooling_soap": "google.comservices/Soap/T/{version}6902", "tooling_rest": "google.comservices/data/v{version}/tooling/", "profile": "google.com0052969023DihTAAS", "feeds": "google.comservices/data/v{version}/chatter/feeds", "groups": "google.comservices/data/v{version}/chatter/groups", "users": "google.comservices/data/v{version}/chatter/users", "feed_items": "google.comservices/data/v{version}/chatter/feed-items", "feed_elements": "google.comservices/data/v{version}/chatter/feed-elements", "custom_domain": "https://googlebrazil--googleRSIT6.cs19.my.salesforce.com" }, "active": true, "user_type": "CSP_LITE_PORTAL", "language": "pt_BR", "locale": "pt_BR", "utcOffset": -1086902, "last_modified_date": "2019-05-15T14:40:14Z", "is_app_installed": true, "custom_attributes": { "SAN": "google69020345668", "lastName": "Order1", "firstName": "R 5.9" } }

Policy is :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-SAN"> <DisplayName>Extract SAN</DisplayName> <JSONPayload> <Variable name="SAN"> <JSONPath>$.custom_attributes.SAN</JSONPath> </Variable> <Variable name="firstName"> <JSONPath>$.custom_attributes.firstName1</JSONPath> </Variable> <Variable name="lastName"> <JSONPath>$.custom_attributes.lastName</JSONPath> </Variable> </JSONPayload> <Source clearPayload="false">request</Source> <VariablePrefix>reqPrefix</VariablePrefix> </ExtractVariables>

This is not working. it is giving me error.

Your code works for me. I dont see any error. It populated all the variables except for firstName as your jsonpath points to firstName1. Hope you are passing the Content-Type header too

I delibrately set it to firstName1 as i wanted to see whether it throws an error or not.

and i'm getting below error :

{ "fault": { "faultstring": "Invalid JSON path $.custom_attributes.firstName1 in policy Extract-SAN.", "detail": { "errorcode": "steps.extractvariables.InvalidJSONPath" } } }

if Content-Type header is not coming as application/json then it wouldn't have fetch SAN as well.

I see you are getting SAN's value. Not sure why its failing. I have included my proxy. Works for me

test-extract-json-rev1-2019-05-15.zip