logo
down
shadow

jsonSlurper: get one value based on other value


jsonSlurper: get one value based on other value

By : user2956235
Date : November 22 2020, 10:54 AM
this one helps. use find to get the list element containining your desired key/value pair
code :
println([["level3": "level3Value3", "level4": "level4value3"],
         ["level3": "level3Value4", "level4": "level4value4"],
        ].find{ it.level4=='level4value4' }.level3)


Share : facebook icon twitter icon
JsonSlurper returns No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.

JsonSlurper returns No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.


By : BosssDesign
Date : March 29 2020, 07:55 AM
With these it helps According to the documentation, the HTTPBuilder could be parsing your JSON for you. If your JSON response has its root as a JSON array, then that explains the ArrayList object in your reader variable.
Regarding how this explains the exception being thrown. The reader parameter of the Closure is an ArrayList of parsed JSON, not a String of unparsed JSON. Thus, the code fails on new JsonSlurper().parseText(reader) because reader is not text and the JsonSlurper does not have a method defined for how to parse an ArrayList as JSON.
Groovy / jsonSlurper using OR (IN) in assertion

Groovy / jsonSlurper using OR (IN) in assertion


By : Evaldas
Date : March 29 2020, 07:55 AM
To fix this issue , Lower case in with a proper list should work too
code :
assert jsonSlurper.body.message in ["Approved","Aprobada"]
How to use JsonSlurper?

How to use JsonSlurper?


By : รองรับได้ เป็นบางครั
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I have written a test case in SOAP UI which creates a user and returns the Id. This is the JsonResponse. Through a Groovy script, I need to extract the id , It will be:
code :
import groovy.json.JsonSlurper

def json = """
{
    "schemas":["urn:hid:scim:api:ma:1.0:UserInvitation"],
    "urn:hid:scim:api:ma:1.0:UserInvitation":
        [
            {
                "meta":{ 
                    "resourceType":"UserInvitation",                                                   
                    "lastModified":"2015-12-22T07:45:30Z",
                    "location":"https://test-ma.api.assaabloy.com/credential-management/customer/663/invitation/2643209"
                },
                "invitationCode":"FBXO-SRWS-LKFI-ZKZI",
                "status":"PENDING",
                "createdDate":"2015-12-22T02:45:30Z",
                "expirationDate":"2015-12-22T02:45:30Z",
                "id":2643209
             }
         ]
}"""
def slurped = new JsonSlurper().parseText(json)
assert 2643209 == slurped."urn:hid:scim:api:ma:1.0:UserInvitation"[0].id
JsonSlurper execution failure

JsonSlurper execution failure


By : EpicBellyFlop45
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I am trying to use JsonSlurper to input variables from different files. But it is failing at second execution. Could someone help me? , tl;dr: Change JsonSlurper to JsonSlurperClassic.
Answer:
Getting the only key of a Map from JsonSlurper

Getting the only key of a Map from JsonSlurper


By : varat za
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You should be able to use keySet method to get the keys which is a list. Since, you mentioned only key, you can use the first element as shown below:
code :
def jsonString = """{
  "rootKey": []
}"""
def json = new groovy.json.JsonSlurper().parseText(jsonString)
println json.keySet()[0]
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org