How to get filtered json from payload using policies not using database.

Not applicable

I have created json using Assign message policy and want to filter this json on the basis of query parameters .@Anil Sagar

Solved Solved
0 6 1,421
1 ACCEPTED SOLUTION

Filtering a JSON payload is something a backend system typically does. For example when you use Firestore you can just send the query to it and it will perform the filter action.

If you want to do the filtering in Apigee I suggest doing this with a JavaScript policy. It could look something like this.

Let assume the JSON looks like this:

{
 'entities': [
	{'id':0, 'name':'testitem0','type':'test'},
	{'id':1, 'name':'testitem1','type':'test'},
	{'id':2, 'name':'testitem2','type':'other'}
        ]
}

And let's say we want to filter on the type queryparam:

var json = JSON.parse(context.getVariable('response.content'));
var newjson = {'entities':[]};
var typefilter = context.getVariable('request.queryparam.type');

// walk through entities and perform filter
for(var i=0;i<json.entities.length;i++) 
{
  if(json.entities[i].type == typefilter) 
  {
    newjson.entities.push(json.entities[i]);   
  }
}

// write new filtered JSON
context.setVariable('response.content',JSON.stringify(newjson));

View solution in original post

6 REPLIES 6

@Rajwinder kaur

If I understand you correctly you have hardcoded the JSON response in the API Proxy by use of the Assign Message policy. This API Proxy is not calling any backend system?

How do you want to filter it exactly? Do you have an example of your created json, and how you want to apply filtering to it?

It seems like something that would be done with a javascript policy that would read in your json and query parameters, then do the needful.

Filtering a JSON payload is something a backend system typically does. For example when you use Firestore you can just send the query to it and it will perform the filter action.

If you want to do the filtering in Apigee I suggest doing this with a JavaScript policy. It could look something like this.

Let assume the JSON looks like this:

{
 'entities': [
	{'id':0, 'name':'testitem0','type':'test'},
	{'id':1, 'name':'testitem1','type':'test'},
	{'id':2, 'name':'testitem2','type':'other'}
        ]
}

And let's say we want to filter on the type queryparam:

var json = JSON.parse(context.getVariable('response.content'));
var newjson = {'entities':[]};
var typefilter = context.getVariable('request.queryparam.type');

// walk through entities and perform filter
for(var i=0;i<json.entities.length;i++) 
{
  if(json.entities[i].type == typefilter) 
  {
    newjson.entities.push(json.entities[i]);   
  }
}

// write new filtered JSON
context.setVariable('response.content',JSON.stringify(newjson));

Thank you so much .It works for me

Not applicable

I have json like

{

"contents":

[

{ "content_id":"1", "width":"1720", "height":"1050"},

{ "content_id":"2", "width":"1920", "height":"1080"},

]

}

if i hit URl like this http://ur-eval-test.apigee.net/contents?content_id=2

then it should filter data like

{ "content_id":"2", "width":"1920", "height":"1080"}

do this in a JavaScript callout

var content_id = context.getVariable('request.queryparam.content_id');
var originalJson = {
  "contents": [
    { "content_id":"1", "width":"1720", "height":"1050"},
    { "content_id":"2", "width":"1920", "height":"1080"}
  ]
};

var result = originalJson.contents.find(function(item) {
  return item.content_id == content_id; 
});

You can read about the Array.prototype.find function here.