Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard in JsonPath

I am parsing a JSON like below:

jobs1: [
{
  request: {
    body: {
      jobID: "79ceeeff-53b9-4645-80bd-95dfca6fe1e9",
...
jobs2: [
{
  request: {
    body: {
      jobID: "60e7c286-f936-4f96-87bc-6bd55f107514",

And looking for a way to use wildcards in the JSON path.

I am using the RestAssured framework in Java. After executing the code like below:

List<String> ids = get("/state/").path("*.request.body.jobID");
System.out.println(ids);

I expect to get:

[79ceeeff-53b9-4645-80bd-95dfca6fe1e9, 60e7c286-f936-4f96-87bc-6bd55f107514]

But instead I get an exception:

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: *. @ line 1, column 27.
                        *.request.body.jobID
                         ^

I have looked through these tutorials, but nothing seemed to work for me:

https://github.com/json-path/JsonPath

http://goessner.net/articles/JsonPath

How do I correctly use the wildcards in JsonPath?

like image 695
Vlad.Bachurin Avatar asked Feb 04 '23 05:02

Vlad.Bachurin


1 Answers

GOTO http://jsonpath.herokuapp.com/

And give input like green box in the given image.

Your pattern should be like below

*.[*].request.body.jobID

Your JSON should be like below

{
	jobs1: [{
			request: {
				body: {
					jobID: "79ceeeff-53b9-4645-80bd-95dfca6fe1e9"
				}
			}
		}
	],
	jobs2: [{
			request: {
				body: {
					jobID: "60e7c286-f936-4f96-87bc-6bd55f107514"
				}
			}
		}
	]
}

Your will get result like below

[
   "79ceeeff-53b9-4645-80bd-95dfca6fe1e9",
   "60e7c286-f936-4f96-87bc-6bd55f107514"
]

enter image description here

like image 65
Zenith Avatar answered Feb 06 '23 18:02

Zenith