retrieve the value where test append in the below request JSON payload

snehansu
Participant III

Hi ,

I have requirement to retrieve the value where test append in the below request payload.

I am able to do it using java script . But I am not able to do using Extract variable in Json Path. Can some one please suggest.

Request Payload:

{ "cardDetails": { "test.accountNo": "XXXXXXX", "expiry": { "test.year": "XXXXXXX", "test.month": "XXXXXXXXX" }, "cardNo": "XXXXX" },

EDIT

Actually my requirement is this: the input payload is JSON. For any field in the JSON with a name that begins with test , mask that field.

I tried to use EV policy instead of using an iterator in java script. But I couldn't make it happen using ExtractVariables policy. Is it ok to use this kind of code in Java script ? Please suggest.

Object.keys(payload).forEach(function(k) { 
  var value = payload[k];
  if (typeof value == "object") { 
    iterateObject(value); 
  }
  if(k.indexOf("test.")>=0) { print() }
});
Solved Solved
0 7 339
1 ACCEPTED SOLUTION

It's fine to use that kind of JavaScript in a JavaScript callout.

Also I think recursion is the right approach to walk the JSON graph. But maybe you need to be a little more thorough.

This works for me.

function wantMaskField(fieldName){
  return (fieldName.indexOf("test.")>=0) || fieldName === 'cardNo';
}


function mask(obj, alwaysMask) {
  var output = {};
  Object.keys(obj).forEach(function(k) {
    var value = obj[k];
    var startsWithTest = alwaysMask || wantMaskField(k);
    if (typeof value == "object") {
      output[k] = mask(value, startsWithTest);
    }
    else if(startsWithTest) {
      output[k] = '****';
    }
    else output[k] = value;
  });
  return output;
}

Use it like this:

var payload = JSON.parse(context.getVariable('request.payload'));
var masked = mask(payload);

For an input like this:

{
  "cardDetails": {
    "accountNo": "1237468",
    "expiry": {
      "test.year": "2018",
      "test.month": "09"
    },
    "cardNo": "4411-0000-0000-0000"
  }
}

You will get this:

{
  "cardDetails": {
    "accountNo": "1237468",
    "expiry": {
      "test.year": "****",
      "test.month": "****"
    },
    "cardNo": "****"
  }
}

You can change the wantMaskField() function to be whatever you like.

View solution in original post

7 REPLIES 7

sidd-harth
Participant V

Hi @MiracleDemo, use <JSONPath>$.cardDetails.expiry.['test.year']</JSONPath>

<?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>
   
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="name">
            <JSONPath>$.cardDetails.expiry.['test.year']</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">request</Source>
    <VariablePrefix>apigee</VariablePrefix>
   
</ExtractVariables>

Thanks Siddharth for the update.

I forgot to update one more thing...requirement is "Read the payload from json request to check if element test exists or not , if exist get the value"

I did using below java script but its taking long time , so I want to use any EV policies or any other policies. Please suggest.

Object.keys(payload).forEach(function(k) { var value = payload[k]; if (typeof value == "object") { iterateObject(value); } if(k.indexOf("test.")>=0) { print() } }.

Any one havaing any suugestion ?

Did you try Siddharth's ExtractVariables policy? And? did it meet your requirement? If it did not, please state what else it needs to do.

What do you mean by "it is taking a long time." Please give some explicit data. How much time does the JS take, and under what load and concurrency. Data please!

Also, be careful of relying on the response time offered in the Trace UI for 1 transaction. Real performance data requires a load test.

Hi Dino, thank you so much for your response. I did not perform any load test. Actually my requirement is , if any data is coming with test in request payload , to mask it. while doing some analysis , I feel to use EV policy instead of using iterator in java script. But I couldn't make it happen using Extract variable policy. I need you suggestion, is it fine if this piece of code to use in Java script ? Please suggest.

Object.keys(payload).forEach(function(k) { var value = payload[k]; if (typeof value == "object") { iterateObject(value); } if(k.indexOf("test.")>=0) { print() } }.

Request Payload:

{ "cardDetails": { "test.accountNo": "XXXXXXX", "expiry": { "test.year": "XXXXXXX", "test.month": "XXXXXXXXX" }, "cardNo": "XXXXX" },

It's fine to use that kind of JavaScript in a JavaScript callout.

Also I think recursion is the right approach to walk the JSON graph. But maybe you need to be a little more thorough.

This works for me.

function wantMaskField(fieldName){
  return (fieldName.indexOf("test.")>=0) || fieldName === 'cardNo';
}


function mask(obj, alwaysMask) {
  var output = {};
  Object.keys(obj).forEach(function(k) {
    var value = obj[k];
    var startsWithTest = alwaysMask || wantMaskField(k);
    if (typeof value == "object") {
      output[k] = mask(value, startsWithTest);
    }
    else if(startsWithTest) {
      output[k] = '****';
    }
    else output[k] = value;
  });
  return output;
}

Use it like this:

var payload = JSON.parse(context.getVariable('request.payload'));
var masked = mask(payload);

For an input like this:

{
  "cardDetails": {
    "accountNo": "1237468",
    "expiry": {
      "test.year": "2018",
      "test.month": "09"
    },
    "cardNo": "4411-0000-0000-0000"
  }
}

You will get this:

{
  "cardDetails": {
    "accountNo": "1237468",
    "expiry": {
      "test.year": "****",
      "test.month": "****"
    },
    "cardNo": "****"
  }
}

You can change the wantMaskField() function to be whatever you like.

Thank you Dino.