json range operator (:) not working

The JSONPath range select operator `:` does seem to work:

For example, I have this payload

{
  "Products": [{
    "id": "1",
    "name": "cheese"
  }, {
    "id": "2",
    "name": "ham"
  }, {
    "id": "3",
    "name": "apple"
  }, {
    "id": "4",
    "name": "orange"
  }, {
    "id": "5",
    "name": "crackers"
  }, {
    "id": "6",
    "name": "ice cream"
  }]
}

I applied

< JSONPath>$.Products.[1:3]</JSONPath> in the extractVariable

I am expecting the following:

[ { "id": "2", "name": "ham" }, { "id": "3", "name": "apple" } ]

But I am getting the following error instead.

{
  "fault": {
    "faultstring": "ExtractVariables EV-ApplyProductsListOffset: Json path parsing failed for for flow variables response.content",
    "detail": {
      "errorcode": "steps.extractvariables.JsonPathParsingFailure"
    }
  }
}

I checked my JSONPath Syntax at http://jsonpath.com/ , and I am getting the expected result.

I am seriously pazzled as to why it does not work in Apigee.

I am wondering if the JSONPath support in the ExtractVariable only partial?

Any help would be much appreciated.

0 4 581
4 REPLIES 4

I think the problem is that Apigee is using an older version of the json-path module, which perhaps does not support the array slice syntax ([1:3]) .

Could you work around the problem by using [1,2] ?

In the meantime we can look into updating the json-path module.


EDIT: confirmed - this is the problem. b/70937331 . There's a fix in the pipeline.

bug id: b/70937331

Thanks @Dino, I also found that out via https://jsonpath.curiousconcept.com/ Looks like Apigee is usj g 0.2.x or below. this feature is available from version 0.3.x onward.

I was intended to use it for simple pagination purposes. The way I work around it is by using using the javascript function slice(). I guess the slice function in javascript policy is the easiest way work around it.

var allProducts=context.getVariable("response.content");

//Get Offset from variable

var offset = parseInt(context.getVariable("pagination.offset"));

//Get limit from variable

var limit = parseInt(context.getVariable("pagination.limit"));

var endIndex = offset+limit;

allProducts = JSON.parse(allProducts);

var pageProducts=allProducts.Products.slice(offset, endIndex);

var paginatedResponse = JSON.stringify(pageProducts);

context.setVariable("paginatedResponse",paginatedResponse)

ok, got it.

Yes, we're working on updating the json-path.