Extract Variable from JSON payload with URL as element

I have a json payload that I'm trying to extract variables where the names of some of the objects are url's:

 

{
	"sub":"subject@clients",
	"aud":"https:\/\/identity.testwebsite.com",
	"azp":"subject",
	"scope":"partner-limited",
	"iss":"https:\/\/testwebsite-uat.auth0.com\/",
	"https:\/\/www.testwebsite.com\/permissions":
		["partner-limited"],
	"exp":1630578501,
	"https:\/\/www.testwebsite.com\/partner":
		{
			"customer_id":"4321"
		},
	"iat":1630556901,
	"gty":"client-credentials"
}

 

My extract variables policy is able to extract variables from any of the other objects but I can not extract the customer_id. I have tried both with and without escaping the /

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-x-account-id">
    <DisplayName>EV-x-account-id</DisplayName>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="legacy_client_id">
            <JSONPath>$."https:\/\/www.testwebsite.com\/partner.customer_id</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">jwt.Verify-JWT-1.payload-json</Source>
</ExtractVariables>

 

and 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-x-account-id">
    <DisplayName>EV-x-account-id</DisplayName>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="legacy_client_id">
            <JSONPath>$.https://www.testwebsite.com/partner.customer_id</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">jwt.Verify-JWT-1.payload-json</Source>
</ExtractVariables>

 

However I am able to pull all the objects that are not url's, for example :

 

<JSONPath>$.sub</JSONPath>

 

returns the expected value. Any idea how I retrieve these variables? 

Solved Solved
0 3 1,071
1 ACCEPTED SOLUTION

Hi @ncastellanos 

I tried with the following policy and it worked

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables continueOnError="false" enabled="true" name="EV-GetCustomerId">
    <DisplayName>EV-GetCustomerId</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="customerId">
            <JSONPath>$['https://www.testwebsite.com/partner']['customer_id']</JSONPath>
        </Variable>
        <Variable name="permissions" type="nodeset">
            <JSONPath>$['https://www.testwebsite.com/permissions']</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">request</Source>
    <VariablePrefix>reqPrefix</VariablePrefix>
</ExtractVariables>

I have included the snippet to extract the permissions array as well. Hope this works for you

View solution in original post

3 REPLIES 3

Hmmm, ya, I am unsure if the Apigee runtime allows environment variables to have names that contain slashes.

A couple things for you. 

First, the VerifyJWT policy automatically sets those variables. If you don't already have a specific variable for the things that have URLs as fieldnames, then.... it probably won't work. 

To satisfy your curiosity, You could try this again with something like this: 

<Javascript timeLimit="200" name="JS-ShredPayloadJson">
    <Source>
        var c = context.getVariable('jwt.Verify-JWT-1.payload-json');
        c = JSON.parse(c);
        for (var prop in c) {
          context.setVariable('extracted.' + prop, c[prop]);
        }
    </Source>
</Javascript>

But what are you really trying to do? Aside from "Set a variable from a field in the JSON" , what is the larger need or desire?  What do you want to do with the variable once it is set? Can you provide more context?

 

Hi @ncastellanos 

I tried with the following policy and it worked

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables continueOnError="false" enabled="true" name="EV-GetCustomerId">
    <DisplayName>EV-GetCustomerId</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="customerId">
            <JSONPath>$['https://www.testwebsite.com/partner']['customer_id']</JSONPath>
        </Variable>
        <Variable name="permissions" type="nodeset">
            <JSONPath>$['https://www.testwebsite.com/permissions']</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">request</Source>
    <VariablePrefix>reqPrefix</VariablePrefix>
</ExtractVariables>

I have included the snippet to extract the permissions array as well. Hope this works for you

 Thanks, That worked for me too!