5

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?

Vlad.Bachurin
  • 1,340
  • 1
  • 14
  • 22
  • Have you tried with `children` instead of the `*`? Cf. https://github.com/rest-assured/rest-assured/wiki/GettingStarted#jsonpath and http://groovy-lang.org/processing-xml.html#_gpath – D. Kovács Jun 21 '17 at 14:46
  • @D.Kovács `"children().request.body.jobID"` gives `IllegalArgumentException: No signature of method: java.util.HashMap.children() is applicable for argument types: () values: []` – Vlad.Bachurin Jun 21 '17 at 14:53
  • 1
    I'm also trying to figure out how to use wildcards. But if you need a temp solution, the below works. def jsonSlurper = new JsonSlurper(); def jsonResponse = jsonSlurper.parseText(sampleJson); def listOfJobIds = []; jsonResponse.keySet().each{node -> listOfJobIds.add(jsonResponse[node].request.body.jobID[0]); } log.info listOfJobIds; – canpan14 Jun 22 '17 at 19:23

3 Answers3

5

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

Zenith
  • 1,037
  • 1
  • 10
  • 21
2

*.[*].request.body.jobID this will extract the jobID's alone like below

[  
   "79ceeeff-53b9-4645-80bd-95dfca6fe1e9",
   "60e7c286-f936-4f96-87bc-6bd55f107514"
]
Minisha
  • 2,117
  • 2
  • 25
  • 56
1

Do you really need wildcards here? This seems to be able to retrieve what you expect:

..request.body.jobID

Or even simpler:

..jobID
Mykola Tarasyuk
  • 590
  • 4
  • 12