Parsing JSON on Jenkins Pipeline (groovy) - json

I created a method as shown online:
#NonCPS
def parseJsonString(String jsonString) {
def lazyMap = new JsonSlurper().parseText(jsonString)
// JsonSlurper returns a non-serializable LazyMap, so copy it into a regular map before returning
def m = [:]
m.putAll(lazyMap)
return m
}
But I get the following error:
ERROR: java.io.NotSerializableException: groovy.json.internal.LazyMap
To work around this, I have to create an entire method to perform an entire step. For instance, in a method, I would do the same as above, parse the information I want, and finally return it as a string.
This, however, presents another issue, especially if you wrap this method inside a withCredentials, which would then require another withCredentials.

I finally find a BETTER solution!
readJSON() method from the Jenkins "Pipeline Utility Steps" plugin as shown here:
https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readjson-read-json-from-files-in-the-workspace
Here is a sample where we can finally ditch that ugly GROOVY JSONPARSE crap.
node() {
stage("checkout") {
def jsonString = '{"name":"katone","age":5}'
def jsonObj = readJSON text: jsonString
assert jsonObj['name'] == 'katone' // this is a comparison. It returns true
sh "echo ${jsonObj.name}" // prints out katone
sh "echo ${jsonObj.age}" // prints out 5
}
}

Related

Kotlin unable to request JSON files with more than one directory in URL?

While trying to download and parse JSON files through Kotlin, it kept failing trying to access the document, though trying different (shorter) URLS seemed to work fine
val string = "http://ddragon.leagueoflegends.com/cdn/11.2.1/data/en_GB/champion.json"
val client = OkHttpClient()
val request = Request.Builder().url(string).build()
client.newCall(request).enqueue(object: Callback
{
override fun onResponse(call: Call, response: Response) {
val body = response.body?.string()
println(body)
}
override fun onFailure(call: Call, e: IOException) {
println("Failed")
}
})
This gives the "Failed" output in the console:failed#1
However, using a shorter URL such as this:
val string = "https://ddragon.leagueoflegends.com/api/versions.json"
Gives the correct output: working #1
Anyone know why and/or a fix to this?
Thanks!
Update:
Trying with a file that is considerably smaller than the first, but includes two directories instead of one:
val string = "http://static.developer.riotgames.com/docs/lol/maps.json"
Still ends up failing leading me to believe it is unable to access the document if it is too nested within directories?

facing difficulties in Json parsing in groovy

I have a json and its response is {"canMerge":false,"conflicted":true,"outcome":"CONFLICTED","vetoes":[{"summaryMessage":"Requires approvals","detailedMessage":"You need 2 more approvals before this pull request can be merged."}]}
and I want to filter out data on the basis of "outcome":"CONFLICTED" for this I have tried following ways-
def mergeResponse = readJSON file:mergeFileName
for(mergenew in mergeResponse.values)
{
if(mergenew.outcome == "CONFLICTED") {
echo "pull request can not merged"
}
when I am trying above it is skipping if loop directly eventhough condition match properly I am not getting why?
ALSO TRIED BELOW
import groovy.json.JsonSlurper
def slurper = new JsonSlurper().parseText(mergenew)
assert slurper.outcome == "CONFLICTED"
String id = mergenew.getString("id");
echo "pull request can not merged"
getting error for above is
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseFile() is applicable for argument types: (org.apache.commons.collections.map.ListOrderedMap$ListOrderedMapEntry) values: [canMerge=false]
Possible solutions: parse([B), parse([C), parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader), parse(java.net.URL)
I also approved script in jenkins script approval for JsonSlurper.parseText
Please help me. Any help is appreciated.
try this : (set the file name var to whatever the file is)
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
data = jsonSlurper.parse(new File(mergeFileName))
assert data.outcome == "CONFLICTED"
println("pull request can not merged")
You can use Pipeline Utility Steps to read a json file. This works for me:
def data = readJSON file: "${WORKSPACE}/data.json" // here just read the json file
def outcome = "${data.outcome}"
if(outcome == "CONFLICTED") {
echo "pull request can not merged"
}

Parsing JSON object in Groovy

Im Trying to write a Groovy script which performs a REST api call and gets an JSON object, then, i need to get a specific string out of this JSON and check if it matches another string that i provides in the script.
i did everything until the section of comparison,
the string that im getting from the JSON looks like
[AAAAAA/BBBBBB/CCCCCC/file.txt]
and this is my Groovy script:
/*Import Section*/
//--------------//
groovy.json.*
/*Var Declaration*/
//---------------//
String errMessage = "There were no Junit tests impacted in this PR,
Comparison is: "
String scssMessage = "There were Junit tests impacted in this PR,
Comparison is: "
usr= "USERNAME"
pass= "PASSWORD"
pr_num = 92
String validPath = "AAAAAA/BBBBBB/CCCCCC/DDDDDD/EEEEEE"
def copmarison = !false
/*REST API call*/
//------------//
url= "http://{$usr}:
{$pass}#XX.XXX.X.XX:PPPP/rest/api/1.0/projects/XX/repos/TTTTT/pull-
requests/+{$pr_num}/changes?changescope"
process = [ 'bash', '-c', "curl ${url}" ].execute()
process.waitFor()
/*JSON parsing*/
//------------//
def info = new JsonSlurper().parseText(process.text)
def path = info.values.path.toString
/*Impacted JUNIT verifycation*/
//---------------------------//
if(path==validPath){
println ("$scssMessage"+"Valid"+"\n")
comparison=true
}else{
println ("$errMessage"+"Not Valid"+"\n")
comparison=!false
}
Im sure that my comparison isnt good and im looking to compare and find
if part of my "path" contained in my "validPath".
for example, the following case means true:
AAAAAA/BBBBBB/CCCCCC/file.txt
contained in:
AAAAAA/BBBBBB/CCCCCC/DDDDDD/EEEEEE
and i need to find a way to make this comparison
please help
If I understand your question correctly, all you need to check is the path part of your file against a specific path. In that case, this will work:
// you should already have these; including here for clarity
​def file = "AAAAAA/BBBBBB/CCCCCC/file.txt"
def path = "AAAAAA/BBBBBB/CCCCCC/DDDDDD/EEEEEE"
return path.contains(file[0..file.lastIndexOf("/")]​​​)​

problems with handeling Json request in play

I'm trying to make server which can handle HTTP requests with Json. Sorry if I haver. I'm just still new to all this.
So, I made function which takes JsValue and working with it.
def find(info: JsValue) = {
val req = Search.makeRequest("person",info)
val result = DB.withConnection { implicit c =>
SQL(req).as(person *)
}
Json.toJson(result)
}
Then I do something like this:
val test = Json.parse("""{"name":"John"}""")
Person.find(test)
It works fine. But then I try to call this function with HTTP request:
routes file:
GET /findperson controllers.PersonController.findPerson(info: String)
controller:
def findPerson(info: String) = Action {
Ok(Person.find(Json.parse(info)))
}
actual request:
http://localhost:9000/findperson?info="""{"name":"John"}"""
I get:
Exception: Malformed JSON: Got a sequence of JsValue outside an array or an object.
Can someone tell me how do it right? Please.
Although I agree with #Ryan that it is an unusual thing to want to do, I think the problem you are having is actually as the message says, "Malformed JSON". Remember that your url parameter is not source code, it's a simple string. There's no need to escape the quotation marks. So try using the url:
http://localhost:9000/findperson?info={"name":"John"}

Grails: Easy and efficient way to parse JSON from a Request

Please pardon me if this is a repeat question. I have been through some of the questions/answers with a similar requirement but somehow got a bit overwhelmed and confused at the same time. My requirement is:
I get a JSON string/object as a request parameter. ( eg: params.timesheetJSON )
I then have to parse/iterate through it.
Here is the JSON that my grails controller will be receiving:
{
"loginName":"user1",
"timesheetList":
[
{
"periodBegin":"2014/10/12",
"periodEnd":"2014/10/18",
"timesheetRows":[
{
"task":"Cleaning",
"description":"cleaning description",
"paycode":"payCode1"
},
{
"task":"painting",
"activityDescription":"painting description",
"paycode":"payCode2"
}
]
}
],
"overallStatus":"SUCCESS"
}
Questions:
How can I retrieve the whole JSON string from the request? Does request.JSON be fine here? If so, will request.JSON.timesheetJSON yield me the actual JSON that I want as a JSONObject?
What is the best way to parse through the JSON object that I got from the request? Is it grails.converters.JSON? Or is there any other easy way of parsing through? Like some API which will return the JSON as a collection of objects by automatically taking care of parsing. Or is programatically parsing through the JSON object the only way?
Like I said, please pardon me if the question is sounding vague. Any good references JSON parsing with grails might also be helpful here.
Edit: There's a change in the way I get the JSON string now. I get the JSON string as a request paramter.
String saveJSON // This holds the above JSON string.
def jsonObject = grails.converters.JSON.parse(saveJSON) // No problem here. Returns a JSONObject. I checked the class type.
def jsonArray = jsonArray.timesheetList // No problem here. Returns a JSONArray. I checked the class type.
println "*** Size of jsonArray1: " + jsonArray1.size() // Returns size 1. It seemed fine as the above JSON string had only one timesheet in timesheetList
def object1 = jsonArray[1] // This throws the JSONException, JSONArray[1] not found. I tried jsonArray.getJSONObject(1) and that throws the same exception.
Basically, I am looking to seamlessly iterate through the JSON string now.
I have wrote some code that explains how this can be done, that you can see below, but to be clear, first the answers to your questions:
Your JSON String as you wrote above will be the contents of your POST payload to the rest controller. Grails will use its data binding mechanism to bind the incomming data to a Command object that your should prepare. It has to have fields corresponding to the parameters in your JSON String (see below). After you bind your command object to your actual domain object, you can get all the data you want, by simply operating on fields and lists
The way to parse thru the JSON object is shown in my example below. The incomming request is esentially a nested map, with can be simply accessed with a dot
Now some code that illustrates how to do it.
In your controller create a method that accepts "YourCommand" object as input parameter:
def yourRestServiceMethod (YourCommand comm){
YourClass yourClass = new YourClass()
comm.bindTo(yourClass)
// do something with yourClass
// println yourClass.timeSheetList
}
The command looks like this:
class YourCommand {
String loginName
List<Map> timesheetList = []
String overallStatus
void bindTo(YourClass yourClass){
yourClass.loginName=loginName
yourClass.overallStatus=overallStatus
timesheetList.each { sheet ->
TimeSheet timeSheet = new TimeSheet()
timeSheet.periodBegin = sheet.periodBegin
timeSheet.periodEnd = sheet.periodEnd
sheet.timesheetRows.each { row ->
TimeSheetRow timeSheetRow = new TimeSheetRow()
timeSheetRow.task = row.task
timeSheetRow.description = row.description
timeSheetRow.paycode = row.paycode
timeSheet.timesheetRows.add(timeSheetRow)
}
yourClass.timeSheetList.add(timeSheet)
}
}
}
Its "bindTo" method is the key piece of logic that understands how to get parameters from the incomming request and map it to a regular object. That object is of type "YourClass" and it looks like this:
class YourClass {
String loginName
Collection<TimeSheet> timeSheetList = []
String overallStatus
}
all other classes that are part of that class:
class TimeSheet {
String periodBegin
String periodEnd
Collection<TimeSheetRow> timesheetRows = []
}
and the last one:
class TimeSheetRow {
String task
String description
String paycode
}
Hope this example is clear enough for you and answers your question
Edit: Extending the answer according to the new requirements
Looking at your new code, I see that you probably did some typos when writting that post
def jsonArray = jsonArray.timesheetList
should be:
def jsonArray = jsonObject.timesheetList
but you obviously have it properly in your code since otherwise it would not work, then the same with that line with "println":
jsonArray1.size()
shuold be:
jsonArray.size()
and the essential fix:
def object1 = jsonArray[1]
shuold be
def object1 = jsonArray[0]
your array is of size==1, the indexing starts with 0. // Can it be that easy? ;)
Then "object1" is again a JSONObject, so you can access the fields with a "." or as a map, for example like this:
object1.get('periodEnd')
I see your example contains errors, which lead you to implement more complex JSON parsing solutions.
I rewrite your sample to the working version. (At least now for Grails 3.x)
String saveJSON // This holds the above JSON string.
def jsonObject = grails.converters.JSON.parse(saveJSON)
println jsonObject.timesheetList // output timesheetList structure
println jsonObject.timesheetList[0].timesheetRows[1] // output second element of timesheetRows array: [paycode:payCode2, task:painting, activityDescription:painting description]