Daniel
Daniel

Reputation: 11

Jayway jsonpath starting with "/" in regex

I am having issues with jayway jsonpath when it comes to using regex which has xpath in it. Here is my json:

{
  "status": "success",
  "resources": [
    {
      "id": "Attachment-20796b26-7a16-4a3d-b53c-b30d9f3cb798.TXT",
      "name": "1996032897__test1.CREDITPRINTFILE",
      "mimeType": "text/plain",
      "url": "https://test.com"
    }
  ]
}

Here is my expression: resources[?(/__test1.CREDITPRINTFILE/.test(@.name))]

I am getting the below error:

Exception in thread "main" com.jayway.jsonpath.InvalidPathException: Expected path node
    at com.jayway.jsonpath.internal.filter.ValueNode.asPathNode(ValueNode.java:53)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.readExpression(FilterCompiler.java:204)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalANDOperand(FilterCompiler.java:189)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalAND(FilterCompiler.java:151)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalOR(FilterCompiler.java:131)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalANDOperand(FilterCompiler.java:184)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalAND(FilterCompiler.java:151)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalOR(FilterCompiler.java:131)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.compile(FilterCompiler.java:77)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.compile(FilterCompiler.java:53)
    at com.jayway.jsonpath.internal.path.PathCompiler.readFilterToken(PathCompiler.java:461)
    at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:141)
    at com.jayway.jsonpath.internal.path.PathCompiler.readPropertyOrFunctionToken(PathCompiler.java:237)
    at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:151)
    at com.jayway.jsonpath.internal.path.PathCompiler.readDotToken(PathCompiler.java:171)
    at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:145)
    at com.jayway.jsonpath.internal.path.PathCompiler.readContextToken(PathCompiler.java:124)
    at com.jayway.jsonpath.internal.path.PathCompiler.compile(PathCompiler.java:58)
    at com.jayway.jsonpath.internal.path.PathCompiler.compile(PathCompiler.java:75)
    at com.jayway.jsonpath.JsonPath.<init>(JsonPath.java:101)
    at com.jayway.jsonpath.JsonPath.compile(JsonPath.java:467)
    at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:87)
    at daniel.TestJsonPath.main(TestJsonPath.java:72)

I tried using the same expression in online evaluation tools as well: https://jsonpath.herokuapp.com/

Jayway - gives the same error - Expected path node Gatling, Nebhale - errors out Goessner - gives me the valid result

I am using the latest version of jayway i think -

<dependency>
  <groupId>com.jayway.jsonpath</groupId>
  <artifactId>json-path</artifactId>
  <version>2.4.0</version>
</dependency>

Question: If jayway does not solve these kind of expressions, is there any other java library which can be used to solve these?

Upvotes: 1

Views: 1733

Answers (1)

riyasvaliya
riyasvaliya

Reputation: 825

Assuming you are looking for the "resource whose name contains __test1.CREDITPRINTFILE", the jsonpath expression resources[?(@.name =~ /.*__test1\.CREDITPRINTFILE/)] will work with Jayway. Tested at https://jsonpath.herokuapp.com/

Upvotes: 0

Related Questions