Extracting Variable from json payload

Below is the json array that is passed to apigee. I want to extract the dynamic value 12345678 from the URL(see BOLD) which is the jsonpayload I get.

{

"myId": 19,

"requestVarParams":{

"14": "afS3shCa/DGOdaGTykNMDhGwYi3pqO9eR5AiTuMBuvg=",

"24": "6a395cbaondote0e2sub424783620a8308333ef8-5-3-0-0",

"22": "/newbranding/12345678/mtic.html" },

"userReferenceId": "6cc8cad6ondotmg2de0sub4172ccapi9a8e2668a0f215ad-7-3-5"

}

How is it doable? Can i make it generic?


Thanks,
Ram

Solved Solved
0 3 7,512
1 ACCEPTED SOLUTION

There will be 2 policies to get what you need.

1- extract the URL that is captured in your JSON payload.

Use ExtractVariable policy [1].

Recommend to watch this 4-minute video to see how the policy is in action.

You could also study sample codes on Github [2].

Applying to your case:

<ExtractVariables name="ParseJsonResponse">
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <VariablePrefix>mock</VariablePrefix>
    <JSONPayload>
        <Variable name="var22">
            <JSONPath>$.requestVarParams.22
            </JSONPath>
        </Variable>
    </JSONPayload>
</ExtractVariables>

2- Once you are able to extract the URL, pass that on to a JS policy [3] and use JS to extract the part between "/newbranding/" and "/mtic.html" as your final variable.

Sample codes shown in [4].

Applying to your case

var var22 = '/newbranding/123455/mtic.html';
var re = /\/newbranding\/([^\/]*)\/mtic.html/;
var result = var22.match(re);
console.log(result);
console.log(result[1]);

Note: I make an assumption that the URL pattern is "/newbranding/{dynamic_content}/mtic.html" . There is also no validation in that code snippet.

Do change your JS regex accordingly to suit the URL pattern if it's more dynamic, and add your own validation / test.

References:

[1] https://docs.apigee.com/api-platform/reference/policies/extract-variables-policy

[2] https://github.com/apigee/api-platform-samples/tree/master/learn-edge/extract-json-payload

[3] https://docs.apigee.com/api-platform/reference/policies/javascript-policy

[4] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

View solution in original post

3 REPLIES 3

There will be 2 policies to get what you need.

1- extract the URL that is captured in your JSON payload.

Use ExtractVariable policy [1].

Recommend to watch this 4-minute video to see how the policy is in action.

You could also study sample codes on Github [2].

Applying to your case:

<ExtractVariables name="ParseJsonResponse">
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <VariablePrefix>mock</VariablePrefix>
    <JSONPayload>
        <Variable name="var22">
            <JSONPath>$.requestVarParams.22
            </JSONPath>
        </Variable>
    </JSONPayload>
</ExtractVariables>

2- Once you are able to extract the URL, pass that on to a JS policy [3] and use JS to extract the part between "/newbranding/" and "/mtic.html" as your final variable.

Sample codes shown in [4].

Applying to your case

var var22 = '/newbranding/123455/mtic.html';
var re = /\/newbranding\/([^\/]*)\/mtic.html/;
var result = var22.match(re);
console.log(result);
console.log(result[1]);

Note: I make an assumption that the URL pattern is "/newbranding/{dynamic_content}/mtic.html" . There is also no validation in that code snippet.

Do change your JS regex accordingly to suit the URL pattern if it's more dynamic, and add your own validation / test.

References:

[1] https://docs.apigee.com/api-platform/reference/policies/extract-variables-policy

[2] https://github.com/apigee/api-platform-samples/tree/master/learn-edge/extract-json-payload

[3] https://docs.apigee.com/api-platform/reference/policies/javascript-policy

[4] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Thank you for the detailed explanation and steps.. Really appreciate it and was very intutive.

clester
Participant I

Similar question, my json is e.g.,

{
    "alertInstanceId": "05985927-ab93-4933-9d62-51744d64af0e",
    "alertName": "403 and 404 Not Found Test Webhook Test",
    "org": "amer-demo13",
    "description": "403 and 404 Not Found Test with Webhook",
    "alertId": "6f8ff505-16db-11ea-ad50-42010a850821",
    "alertTime": "2020-10-05T16:04:11Z",
    "thresholdViolations": {
        "0 - Count": "Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=403 Trigger Value=7 Violation=sustained above 5.0",
        "1 - Count": "Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=404 Trigger Value=7 Violation=sustained above 5.0"
    },
    "thresholdViolationsFormatted": [{
        "metric": "count",
        "proxy": "ALL",
        "region": "us-east1",
        "statusCode": "403",
        "comparisonType": "above",
        "thresholdValue": "5.0",
        "triggerValue": "7",
        "duration": "5 minutes",
        "violation": "sustained above 5.0"
    },{
        "metric": "count",
        "proxy": "ALL",
        "region": "us-east1",
        "statusCode": "404",
        "comparisonType": "above",
        "thresholdValue": "5.0",
        "triggerValue": "7",
        "duration": "5 minutes",
        "violation": "sustained above 5.0"
    }],
    "playbook": "TEST2: Check out the 404 playbook for test"
}

this ExtractVariable segment will give me the contents of "0 - Count",

<JSONPath>$.thresholdViolations.0 - Count</JSONPath>

but I'd like to extract every value in thresholdViolations into a separate variable, not knowing in advance how many items are in that dict, or even what the names of each value in the dict are.

This code will give me a list of the values in thresholdViolations,

<JSONPath>$.thresholdViolations.*</JSONPath>

but then i need to extract each of the items in the list into separate variables. The reason for all of this is that eventually, I'm going to need to create a payload like this:

[{
	"name" : "Count0", 
	"value" : "Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=403 Trigger Value=7 Violation=sustained above 5.0"
},

{
	"name" : "Count1", 
	"value" : "Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=404 Trigger Value=7 Violation=sustained above 5.0"
}
]