How to extract multiple values from the URI in java script.

Not applicable
http://example.com/mail?$filter=(Mail_ID eq 'SS123456' or Mail_ID eq '10021433' or Mail_ID eq 'BB987652')

Given a request like the above, how can I get the values of Mail_ID. i.e; SS123456, 10021433, BB987652 ?

1 1 907
1 REPLY 1

You're asking about how to use Javascript callout within Apigee Edge to do some parsing of a search or query string contained in a URI. There's nothing special in the JavaScript that can be run within Apigee Edge - so you'd just use the regular string and array methods: split, substring, map... that are provided by Javascript.

There is one special part of Apigee Edge that you may not be aware of - and that is the ability of Javascript callouts in Apigee Edge to reference external JS files. For example, there's a nice URI parsing library called URI.js available at medialize.github.io . This can be used within a JS callout. It needs to be referenced like this:

<Javascript name='Javascript-ParseQparam' timeLimit='200' >
  <Properties>
    <!-- to retrieve properties in js code:   properties.prop1 -->
    <Property name='prop1'>value-here</Property>
  </Properties>
  <IncludeURL>jsc://URI.js</IncludeURL>
  <ResourceURL>jsc://parseQparam.js</ResourceURL>
</Javascript>

The parseQparam might look like this:

// parseQparam.js
// ------------------------------------------------------------------
//
// shows how to use URI to grab query params, and then parses them.
//
// created: Mon Nov  9 18:23:02 2015
// last saved: <2015-November-09 18:39:10>


var sampleUrl = "http://example.com/mail?$filter=(Mail_ID eq 'SS123456' or Mail_ID eq '10021433' or Mail_ID eq 'BB987652')";


// URI is from http://medialize.github.io/URI.js, and is included separately via
// this line in the policy:
//   <IncludeURL>jsc://URI.js</IncludeURL>


var uri = URI(sampleUrl);


// get data map:
var search = uri.search(true);
var filter = search.$filter; // "(Mail_ID eq 'SS123456' or Mail_ID eq '10021433' or Mail_ID eq 'BB987652')"
filter = filter.substring(1, filter.length - 1); // remove parens
var parts = filter.split(' or ');  // assumes clean, well-formed logical clauses, no nesting


var ids = parts.map(function(item){
      var pair = item.split(' eq '); // assumes each clause has an eq operation
      return pair[1];
    });


context.setVariable('ids',ids.join(',')); // 'SS123456','10021433','BB987652'

Of course, you'd want to design your JS to be more resilient in the face of formatting abnormalities, like ill-formed conditional statements, and so on.

Parsing a boolean logic statement such as you offer, reliably, is a little more complicated. You shouldn't try doing that with just Javascript split, substring and map functions. It's better to rely on something that is already built and tested. For example, you can use the JSEP library (https://github.com/soney/jsep ) for parsing boolean logic. This library also is usable within an Apigee Edge callout. Include it in the same way you would include the URI.js library I showed above.

Then use it like so:

// customize jsep
jsep.removeBinaryOp("+");
jsep.removeBinaryOp("-");

jsep.addBinaryOp("or", 10);
jsep.addBinaryOp("and", 10);
jsep.addBinaryOp("eq", 100);
jsep.addBinaryOp("ne", 100);

var valueToParse = "Mail_ID eq 'SS123456' or Mail_ID eq '10021433' or Mail_ID eq 'BB987652'";
var parseTree = jsep(valueToParse);

The output would look like this:

{
  "type": "BinaryExpression",
  "operator": "or",
  "left": {
    "type": "BinaryExpression",
    "operator": "or",
    "left": {
      "type": "BinaryExpression",
      "operator": "eq",
      "left": {
        "type": "Identifier",
        "name": "Mail_ID"
      },
      "right": {
        "type": "Literal",
        "value": "SS123456",
        "raw": "'SS123456'"
      }
    },
    "right": {
      "type": "BinaryExpression",
      "operator": "eq",
      "left": {
        "type": "Identifier",
        "name": "Mail_ID"
      },
      "right": {
        "type": "Literal",
        "value": "10021433",
        "raw": "'10021433'"
      }
    }
  },
  "right": {
    "type": "BinaryExpression",
    "operator": "eq",
    "left": {
      "type": "Identifier",
      "name": "Mail_ID"
    },
    "right": {
      "type": "Literal",
      "value": "BB987652",
      "raw": "'BB987652'"
    }
  }
}