use json path in javascript callout

sraina
New Member

Is there an example of way to use json path expression to parse a json payload in javascript callout?

Solved Solved
0 3 996
1 ACCEPTED SOLUTION

Not applicable

Following js might help.

 /*

Usage Notes:
Initialize MaskingUtility to variable
var maskingUtility = new MaskingUtility();
maskingUtility.mask(jsonInputObject, maskElementKeys)


*/
function MaskingUtility() {
	var MASK_ELEMENT_KEYS = "";
	
	this.isMaskable = function(key) {
		if(this.MASK_ELEMENT_KEYS.indexOf(key) >= 0) {
			return true;
		}	
	};
	
	this.isArray = function(what) {
		return Object.prototype.toString.call(what) === '[object Array]';
	};
	
	this.traverseAndMask = function(o, parentKey) {
		for (var i in o) {
			var currentKey = "";
			if (o[i] !== null && typeof(o[i])=="object") {
				//going on step down in the object tree!!
				if(parentKey.endsWith("[")){
					currentKey = parentKey +  "*" + "]";
				} else if(this.isArray(o[i])) {
					currentKey = parentKey + "." + i + "[";
				} else {
					currentKey = parentKey + "." + i;
				}
				this.traverseAndMask(o[i], currentKey);
			} else {
				if(parentKey.endsWith("[")) {
					currentKey = parentKey +  "*" + "]";
				} else {
					currentKey = parentKey + "." + i;
				}
				if(this.isMaskable(currentKey)) {
					o[i]="*************";
				}
			}
		}		
	};
	
	/*
	* Invoke this method to mask data on jsonObject.
	* jsonObject => JSON data or JSON payload
	* maskElementKeys=> A list (String array) of JSONPath expressions that will be evaluated against 
	                    JSON payloads (if any).Any JSONPaths that successfully resolve will result 
	                    in the value of the JSON property being masked 
	                    Ex: ["$.req.PaymentDetails.CardDetail.CardNumber", "$.req.ArrayElm[*].ContactDetails.phone"];
	*/
	this.mask = function (jsonObject, maskElementKeys) {
		this.MASK_ELEMENT_KEYS = maskElementKeys;
		/*To avoid modification on original json, performing deep copy*/
		var copyJsonObj = JSON.parse(JSON.stringify(jsonObject));
		this.traverseAndMask(copyJsonObj, "$");
		return copyJsonObj;
	};
	
}

View solution in original post

3 REPLIES 3

sraina
New Member

Is there an example of way to use json path expression to parse a json payload in javascript callout? For example for the following payload

{ "property" : "value" }

I want the path to come from configurable source, for now lets assume another javascript file (config.js) which has following jsonpath

$.property

I would like to use config.js in my javascript callout mycallout.js to parse json path and mask the value. The modified payload should look like this.

{ "property" : "****" }

Not applicable

Following js might help.

 /*

Usage Notes:
Initialize MaskingUtility to variable
var maskingUtility = new MaskingUtility();
maskingUtility.mask(jsonInputObject, maskElementKeys)


*/
function MaskingUtility() {
	var MASK_ELEMENT_KEYS = "";
	
	this.isMaskable = function(key) {
		if(this.MASK_ELEMENT_KEYS.indexOf(key) >= 0) {
			return true;
		}	
	};
	
	this.isArray = function(what) {
		return Object.prototype.toString.call(what) === '[object Array]';
	};
	
	this.traverseAndMask = function(o, parentKey) {
		for (var i in o) {
			var currentKey = "";
			if (o[i] !== null && typeof(o[i])=="object") {
				//going on step down in the object tree!!
				if(parentKey.endsWith("[")){
					currentKey = parentKey +  "*" + "]";
				} else if(this.isArray(o[i])) {
					currentKey = parentKey + "." + i + "[";
				} else {
					currentKey = parentKey + "." + i;
				}
				this.traverseAndMask(o[i], currentKey);
			} else {
				if(parentKey.endsWith("[")) {
					currentKey = parentKey +  "*" + "]";
				} else {
					currentKey = parentKey + "." + i;
				}
				if(this.isMaskable(currentKey)) {
					o[i]="*************";
				}
			}
		}		
	};
	
	/*
	* Invoke this method to mask data on jsonObject.
	* jsonObject => JSON data or JSON payload
	* maskElementKeys=> A list (String array) of JSONPath expressions that will be evaluated against 
	                    JSON payloads (if any).Any JSONPaths that successfully resolve will result 
	                    in the value of the JSON property being masked 
	                    Ex: ["$.req.PaymentDetails.CardDetail.CardNumber", "$.req.ArrayElm[*].ContactDetails.phone"];
	*/
	this.mask = function (jsonObject, maskElementKeys) {
		this.MASK_ELEMENT_KEYS = maskElementKeys;
		/*To avoid modification on original json, performing deep copy*/
		var copyJsonObj = JSON.parse(JSON.stringify(jsonObject));
		this.traverseAndMask(copyJsonObj, "$");
		return copyJsonObj;
	};
	
}

Thanks it worked.