Questions tagged [jsonslurper]

JsonSlurper is a Groovy class that makes parsing and working with JSON simpler than with Java.

JSON slurper which parses text or reader content into a data structure of lists and maps.

Example usage:

 def slurper = new JsonSlurper()
 def result = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')

 assert result.person.name == "Guillaume"
 assert result.person.age == 33
 assert result.person.pets.size() == 2
 assert result.person.pets[0] == "dog"
 assert result.person.pets[1] == "cat"

For more info see the docs.

123 questions
13
votes
3 answers

Groovy: validate JSON string

I need to check that a string is valid JSON in Groovy. My first thought was just to send it through new JsonSlurper().parseText(myString) and, if there was no exception, assume it was correct. However, I discovered that Groovy will happily accept…
ewok
  • 20,148
  • 51
  • 149
  • 254
12
votes
2 answers

Groovy compare two json with unknown nodes names and values

I have a rest API to test and I have to compare two json responses. Below you can find a structure of the file. Both files to compare should contains the same elements but order might be different. Unfortunately the names, the type (simple, array)…
Michał Surdy
  • 125
  • 1
  • 1
  • 6
5
votes
1 answer

Pull out nested Maps/EntrySets from JSON with Groovy when criteria met

Big trouble out in code-land and thus I am in need of help! Using Groovy and JsonSlurper I am processing JSON of the form below. I am looking for the outside containing element (in my case this all SHOULD be Maps) when a "type" key is set to a…
JoeG
  • 7,191
  • 10
  • 60
  • 105
4
votes
1 answer

Groovy JsonSlurper JSON value with escaped quotation marks

I'm developing a small tool written in Groovy which parses JSON strings in emails. Some of these JSON strings have JSON values that contain escaped quotes. For example: { "hello": "world with \"quotation marks\"" } To parse these strings, I am…
Thylossus
  • 302
  • 1
  • 3
  • 11
4
votes
1 answer

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

I'm trying to parse JSON file with JsonSlurper.parseText but keep getting similar problems. def jsonParse = null def http = new HTTPBuilder(url) http.auth.basic(username, password) http.request(Method.GET) { response.success = { resp, reader…
Tinolover
  • 166
  • 1
  • 5
  • 19
3
votes
2 answers

Add strong typing to objects from JsonSlurper

I'm having some trouble getting typing to work with the JsonSlurper in Groovy. I'm fairly new to Groovy, and even newer to adding strong types to it - bear with me. Right now I've created a trait which defines the general shape of my JSON object,…
Sandy Gifford
  • 7,219
  • 3
  • 35
  • 65
3
votes
1 answer

Using Groovy JsonSlurper to parse an array of objects

Groovy here. I am parsing a file that contains a list of all the countries: { "countries": [ { "id": "1", "sortname": "AF", "name": "Afghanistan" }, { "id": "2", "sortname": "AL", …
smeeb
  • 27,777
  • 57
  • 250
  • 447
3
votes
1 answer

How do I iterate on a Map object returned from JsonSlurper.parse(JSONFile)?

I'm using a Groovy script in Ready!Api 1.9.0 to decode a base64 string which is returned in a SOAP response and store the resulting JSON object in a json file. Then take that resulting file and parse it with JsonSlurper to get a Map object. This…
justAguy88
  • 33
  • 1
  • 1
  • 4
3
votes
1 answer

JsonSlurper execution failure

I am trying to use JsonSlurper to input variables from different files. But it is failing at second execution. Could someone help me? It's failing from step Jenkins_File_Path = readFile ( "${Local_Path_App}" + "/Jenkinsfile" ) Main.groovy import…
Eldo
  • 195
  • 5
  • 13
3
votes
2 answers

How to use variable to extract value from JSON response using Groovy?

I'm trying to extract bicycle brand "Cannondale" from the JSON response by using the location of the value stored in a variable called "jsonFieldName" Alternatively, I'm able to successfully extract the brand value by using following syntax: def…
fambo
  • 173
  • 1
  • 2
  • 13
3
votes
1 answer

How to use JsonSlurper?

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 { "schemas":["urn:hid:scim:api:ma:1.0:UserInvitation"], …
Ekta
  • 338
  • 2
  • 9
  • 26
3
votes
1 answer

Groovy JsonSlurper and nested maps

I have a method that returns fairly-nested JSON such as: [[fizz: buzz, foos: [[count: 4, flim: flam], [count: 6, flim: flume]]]] When I try to use JsonSlurper to slurp this JSON into a def result I am getting exceptions: // json == “[[fizz: buzz,…
smeeb
  • 27,777
  • 57
  • 250
  • 447
2
votes
1 answer

Store multiple key:value pairs inside json object

I am very new with groovy scripts. I would like to build a JSON output using responses from an api that I query. Since I want to build the JSON file dynamically over multiple queries, I am using a map. I want to store multiple key:value pairs in a…
Zepperoni
  • 23
  • 3
2
votes
0 answers

Why does instantiating a JsonSlurper cause not-serializable exception?

This is my Jenkinsfile: @Library('libs@dev') libs pipeline { agent any stages { stage( "json" ) { steps { script { my_lib.jsonslurper() } } } } } This is my groovy shared library: //…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
2
votes
2 answers

SoapUI Groovy - Extract Child Node using json slurper object filter

Snippet From Json Response - [ { "id": "ada2ac5d-57f1-4624-b35a-4fd34ada9e56", "userId": "userid", "firstName": "firstname", "lastName": "lastname", "dateAdded": "2018-07-06T04:19:21.57", "dateOfChange":…
Novice Coder
  • 105
  • 1
  • 6
1
2 3
8 9