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
2
votes
1 answer

Getting the only key of a Map from JsonSlurper

I have JSON that needs to be processed using Groovy. I am pretty sure that the JSON has only one key, with this format: { rootKey: [...] } Where rootKey stands for different values (e.g. "customers", "stores", etc.). Let's say I used…
oikonomiyaki
  • 7,691
  • 15
  • 62
  • 101
2
votes
3 answers

Get JSON children of single node with random name in Groovy

In Groovy (not Grails), I want to get the color of such an item: { "8436": { "color": "red", } } The "8436" number is dynamic, but there is always only one. I can't use JsonSlurper's json.8436.color syntax, because the number would…
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
2
votes
2 answers

reading name of json using groovy

I have the below JSON structure and I am trying to retrieve the name order/sale/Cancel to a string variable in groovy {"Transaction" : {"Order" : { ...... {"Transaction" : {"Sale" : { ...... {"Transaction" : {"Cancel" : { ...... I was able to get…
anuj
  • 21
  • 1
2
votes
2 answers

Groovy + JsonSlurper strange behaviour

I have following code to parse a JSON file: @Override Map parseJson() { Object configurationFile = readConfigurationFile() configurationFile.schemas.each { schemaProtectionInformation -> …
wooki
  • 418
  • 5
  • 14
2
votes
1 answer

Concatenation of JSON Arrays/Objects using Groovy

How can two independent different JSON Arrays or JSON Objects be merged or concatenated and treated as a single JSON Object using Java or Groovy. See below sample JSON independent Objects i have First one holds Duties information [ { "code":…
MaSu
  • 23
  • 2
  • 4
2
votes
2 answers

How to assert json response content using groovy?

in the response of a request i have this content: "comp":[ { "type":"header", "version":1, "settings": {"logo":"mylogo", "logoPosition":"left", "inverseLogosPosition":false, "headerTitle":"My Report", …
kirk douglas
  • 577
  • 5
  • 18
  • 35
1
vote
1 answer

Groovy slurper.parser variable of a variable

Here is the snippet of my groovy script: jsonFileData = slurper.parse(jsonFile) Here is my JSON file { "MEMXYZ": { "LINKOPT": { "RMODE": "31", "AMODE": "ANY" }, "PROCESSOR": "PROCESSOR XYZ", …
Ray Lam
  • 13
  • 3
1
vote
1 answer

How to convert JSON value to escaped String in groovy [JsonSlurper]?

i have a groovy script that runs on Jenkins, i have build there a json object using JsonSlurper The json object is a nested one, i would need to convert the nested json child into escaped string value instead of a json object (that's the requirement…
USer22999299
  • 5,284
  • 9
  • 46
  • 78
1
vote
1 answer

Magicdraw - Groovy Script does not have JsonSlurper?

I tried running this script as a macro on Magicdraw, but kept getting an error at line 142 where the script calls JsonSlurper. Then, when I tried importing the Jsonslurper function from the Json library import groovy.json.JsonSlurper;, it gives me a…
Ethan Levy
  • 21
  • 1
1
vote
2 answers

Preparing JSON array of Objects from multiple array list

I am very new to the Groovy scripts and would like to build a JSON output from the below JSON input. Kindly help! My JSON input looks like this: { "id":"1222", "storageNode": { "uuid": "22255566336", "properties": { "BuinessUnit":…
Praloy
  • 13
  • 4
1
vote
1 answer

How to handle raw JSON strings in JsonSlurper?

I'm currently trying to automate running Postman collections in our CI/CD tool. What I noticed is that, in general, JSON strings that also contain a raw JSON string cannot be parsed. Here's a simple example to demonstrate this. import…
Joseph Woolf
  • 500
  • 5
  • 14
1
vote
1 answer

Groovy parse Json preserve keys order

I try to parse a Json in groovy/Jenkins(I have no access to readJSON step) and keep the json keys order. After my research, I found that groovy Map/HashMap objects are not preserving the order of the keys. The only type which keeping order is…
1
vote
2 answers

How to get value of temperature based on station given?

I need to store value of temperature as Temperature from json payload based on station name "station1" but I always ended up with null. My code : JSONParser parser = new JSONParser() try (Reader reader = new…
meado1992
  • 19
  • 6
1
vote
2 answers

Get type of value within JSON object using Groovy

I'm having the following JSON object which I want to check import groovy.json.JsonSlurper def jsonSlurper = new JsonSlurper() import groovy.json.JsonOutput; def object = jsonSlurper.parseText ''' { "id" : 10, "docType" : "PDF", "values" : { …
William Pham
  • 271
  • 3
  • 17
1
vote
1 answer

How in groovy (java) automatically find out the json field type and return values from json, replacing null with empty space?

The question is still relevant! In my task, json comes to my input, which I do not know in advance. I need to collect all json field types into "types" and return all values ​​using reader.outputLines. Now the list of json field types is formed like…
Ekz0
  • 63
  • 1
  • 8
1
2
3
8 9