JSON to Groovy with JsonSlurper and unknown "string" - json

I am writing a Grails/Groovy app and I have a JSON object with a "string" name (grommet and widget) inside the params member that can change. That is, next time it might be acme and zoom. Here is the JSON:
def jx = """{
"job": "42",
"params": {
"grommet": {"name": "x", "data": "y"},
"widget": { "name": "a", "data": "b"}
}
}"""
I am trying to figure out how to get the string grommet . Code so far:
def dalist = new JsonSlurper().parseText(jx)
println dalist.job // Gives: 42
println dalist.params // Gives: [grommet:[name:x, data:y], widget:[name:a, data:b]]
println dalist.params[0] // Gives: null
Any idea how to get the string grommet? Iama going to keep hitting my head against a wall.

The params key on the JSON object is associated with a JSON object, not an array, so you cannot access it by index. JsonSlurper maps JSON objects to Groovy Maps, so you can access params by its keys, which are strings, e.g. dalist.params.grommet, which will give you the map [name: 'x', data: 'y'].
To access the keys on the params you can do dalist.params.keySet(), which will give you the list ['grommet', 'widget']. If you are interested in just knowing params keys, that should do the trick. If you need to get the 'grommet' string for some reason, you can do it by accessing the first element on that list, i.e. dalist.params.keySet()[0], but i don't know why you would want to know that. And i'm not sure if it is guaranteed that the first key of that map will always be 'grommet', as JSON objects are unordered by the spec (from json.org: An object is an unordered set of name/value pairs), but, in turn, Groovy maps are ordered (the default implementation is LinkedHashMap)... so i would assume that the order is preserved when parsing JSON to the Groovy world, but i'd try not to rely on that particular behavior hehe.

It's Map instance, try:
def params = dalist.params.entrySet() as List // entrySet() returns Set, but it's easier to use it as a List
println params
println params.size()
println params[0]
println params[0].key
println params[0].value

This might help you.
import groovy.json.JsonSlurper;
def jx='{"job":"42","params":{"grommet":{"name":"x","data":"y"},"widget":{"name":"a","data":"b"}}}'
def dalist = new JsonSlurper().parseText( jx )
assert dalist.params.getClass().name == "java.util.HashMap";
assert dalist.params.size() == 2;
def keys = dalist.params.collect{ a, b -> a}; // returns "[grommet, widget]"
assert !!dalist.params.get( "grommet" ) == true

Related

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 LinkedHashMap So I try to convert the output of JsonSlurper.parseText to linkedhashmap but it still changing the items order
def jsonstr = """
{
"Id": 533,
"StartTime": "2022-05-10 11:56:18",
"EndTime": "2022-05-10 11:58:49",
"TimeShift": "N/A",
"Run": "123",
"Setup": "Test",
"Version": "3.17",
"Result": "pass",
"DebugMode": 1,
"NumberOfCores": 3,
}
"""
//init as LinkedHashMap
LinkedHashMap map = new LinkedHashMap()
map = (LinkedHashMap) (new JsonSlurperClassic().parseText(jsonstr))
println(map)
/*
the output is in incorrect order, I expect to get `Id` attribute as a first key but I'm getting:
[EndTime:2022-05-10 11:58:49, Version:3.17, TimeShift:N/A, StartTime:2022-05-10 11:56:18, DebugMode:1, Run:123, NumberOfCores:3, Id:533, Setup:Test, Result:pass]
*/
Here is the solution:
I realized that readJSON step is keeping the order so I try to take a look at its implementation.
readJSON uses net.sf.json.* library, in this library there is an option to parse string to jsonObject (with keeping the order of the keys!) by:
import net.sf.json.JSONSerializer
def map = JSONSerializer.toJSON(jsonstr)
println(map)
NOTES:
if you want use it during a pipeline I suggest you to use readJSON step itself, otherwise, you'll need to approve this function from Manage Jenkins -> script approval
In this method empty properties will be net.sf.json.JSONNull
which holds a textual value "null" -> if (someEmptyKey != null) always returns true (also when null) to avoid that, use:
if (!someEmptyKey instanceof JSONNull )
Sources: docs, jenkins-implementation

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 JsonSlurper:
def map = jsonSlurper.parseText(myjson)
How do I obtain that rootKey string?
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:
def jsonString = """{
"rootKey": []
}"""
def json = new groovy.json.JsonSlurper().parseText(jsonString)
println json.keySet()[0]

Regular expression to match two JSON objects in Ready API

I am implementing a ready api project in which I have to compare two JSON objects.
Say Obj1 = {"A":"Test1","B:"Test2"} is an input.
I have a regular expression inside the script file in which I have
Obj = '''{"A":".*","B":".*"}''' and I tried to do assert obj1 == obj which didn't work.
Could some one tell me if script file in ReadyAPI doesn't support regular expression in such format?
With your example, you can do the comparison by parsing using JsonSlurper and get the keys before comparing as you are not bothered about the values.
See the example script:
def obj1 = """{
"A" : "Test1",
"B" : "Test2"
}"""
def obj2 = """{
"A" : "Test1a",
"B" : "Test2b"
}"""
def getJsonKeys = { jsonString ->
def json = new groovy.json.JsonSlurper().parseText(jsonString)
json.keySet()
}
assert getJsonKeys(obj1) == getJsonKeys(obj2)
Note that there are different values for the keys in obj2. But, just compared only keys.
Also note that if your json has more depth, you may to have to alter solution based on the data. Assuming that you have given right data.
You can quickly try it online demo

Deserialize string value by JSON-lib (net.sf.json)

I have discovered problem in JSON-Lib with deserialization of JSON string data in nested objects. Following code in Groovy demonstrates problem, you can test it in Groovy Console:
#Grab('net.sf.json-lib:json-lib:2.3:jdk15')
import net.sf.json.*
def s = '''\
{
"attributes": "'{test:1}'",
"nested": { "attributes": "'{test:1}'" }
}'''
def j = JSONSerializer.toJSON(s)
println j
assert j.attributes == "{test:1}" // OK
assert j.nested.attributes == "{test:1}" // Failed
In the example above both attributes in JSON have the same string value "{test:1}". JSONSerializer deserialize the first one into String value but second one into (maybe) JSONObject. I'm not sure because class of that object returns null.
For different string value (which doesn't look like JSON data, e.g. "blah") it works.
How can I change this behavior?
UPDATE
I have tried to quote string values based on JSON-Lib documentation here (thanks to #GemK for pointing there), but with same result. Quoting string values doesn't work in nested objects :-(

JSON with duplicate key names losing information when parsed

So either I go back and tell someone that they should fix their JSON, or I need to find out what I am doing wrong. Here is the JSON, notice that parameter occurs three times:
String j= '''{
"jobname" : "test",
"parameters" : {
"parameter": {"name":"maxErrors", "value":"0"},
"parameter": {"name":"case", "value":"lower"},
"parameter": {"name":"mapTable", "value":"1"}
}
} '''
And I am trying to get each name & value. My code
def doc = new JsonSlurper().parseText(j)
def doc1 = doc.entrySet() as List
def doc2 = doc.parameters.entrySet() as List
println "doc1.size===>"+doc1.size()
println "doc1===>"+doc1
println "doc2.size===>"+doc2.size()
println "doc2===>"+doc2
And my results:
doc1.size===>2
doc1===>[jobname=test, parameters={parameter={name=mapTable, value=1}}]
doc2.size===>1
doc2===>[parameter={name=mapTable, value=1}]
How come I only get one parameter? Where are the other two? It looks like JSON only keeps one parameter and discards the other ones.
The JSON is not in the correct format. There should not be duplicate key in the same hierarchy or they will override each other.
It should have been an array of paramters.
Like this,
String j= '''{
"jobname" : "test",
"parameters" : [
{"name":"maxErrors", "value":"0"},
{"name":"case", "value":"lower"},
{"name":"mapTable", "value":"1"}
]
}