JSON Query

Not applicable

I have a JSON response that looks like the below:

{"shows":
    [{"Title":"Star Trek","stub":"startrek","ID":"5678"},
     {"Title":"Star Wars","stub":"starwars","ID":"1234"}]
}

I'm trying to generate a query off of one of the elements in the response, so that if I have a query such as apigee.net/{proxy}?stub=starwars it renders just the array associated with that query, so I'd get :

{"shows":
     [{"Title":"Star Wars","stub":"starwars","ID":"1234"}]

I was trying a JavaScript using context.targetResponse.content.asJSON but it doesn't seem to be working.

var stubResponse = context.targetResponse.content.asJSON.stub;
context.setVariable("stub", stubResponse);

Any recommendations?

Solved Solved
0 2 598
1 ACCEPTED SOLUTION

Not applicable

Got it.

var stubResponse = context.proxyResponse.content.asJSON;
context.setVariable("stubResponse", JSON.stringify(stubResponse));
for (show in stubResponse.shows)  {
  if (stubResponse.shows[show].stub == context.getVariable("request.queryparam.stub")) {
    context.proxyResponse.content.asJSON = stubResponse.shows[show];
    break;
  }
}

View solution in original post

2 REPLIES 2

Not applicable

Got it.

var stubResponse = context.proxyResponse.content.asJSON;
context.setVariable("stubResponse", JSON.stringify(stubResponse));
for (show in stubResponse.shows)  {
  if (stubResponse.shows[show].stub == context.getVariable("request.queryparam.stub")) {
    context.proxyResponse.content.asJSON = stubResponse.shows[show];
    break;
  }
}

Not applicable

Hi Chris,

Great. If your requirements get more complicated, one thing you may consider is using Apigee-127. With A127, the response from your backend can be manipulated directly using standard Node/JavaScript. An A127 app can be uploaded to the Apigee cloud and run like any other proxy.