2

4 for checking the status of the process using httpbuilder and getting the below response.

  {"result":[{"id":"167687","dapadmin":"false","status":"in progress","lastupdated":"2017-04-21 14:34:30.0","started":"2017-04-21 14:34:28.0","user":"sys","log":"Running a Stop action\n\nRunning command \n"}]}

Am unable to parse this response using jsonSlurper.parseText() giving error

When I use `

def json = JsonOutput.toJson(statusresponse)    
println JsonOutput.prettyPrint(json)    

I could see it is printed as json object

{
    "result": [
        {
            "id": "167687",
            "dapadmin": "false",
            "status": "in progress",
            "lastupdated": "2017-04-21 14:34:30.0",
            "started": "2017-04-21 14:34:28.0",
            "user": "dapsystem",
            "log": "Running a Stop action\n\nRunning command \n"
        }
    ]
}    

When i check the getClass() , it prints as java.lang.String.

I need to get the lastupdated and status , id values from this response. Please help me to find a solution for this.

Many thanks

Hi Yanpei.

Thanks for the response.

I am using the below code as suggested by you.

    def statusresponse = http.putText(baseurl,path,query,headerMap, Method.GET)
             println("The status response value is"  )
             statusresponse.each{ k, v -> 
                 println "${k}:${v}" 
             }

             def json = JsonOutput.toJson(statusresponse)
              println "JSON Object List : " + json
              println "------------------"
                              println JsonOutput.prettyPrint(json)
                println "The result class  is "+json.getClass()
                println "The result status  is "+(json instanceof Map)


                //def entry = slurper.parseText(json)

                def slurper = new groovy.json.JsonSlurper()
                def entry = slurper.parseText(statusresponse)
                def lastupdated = entry.result.lastupdated
                checkStatus = entry.result.status
                def id = entry.result.id        

Am getting the below error

       The result class  is class java.lang.String
        Caught: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.HashMap) values: [[result:[[id:170089, dapadmin:false, status:in progress, ...]]]]
        Possible solutions: parseText(java.lang.String), parse(java.io.Reader)
groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.HashMap) values: [[result:[[id:170089, dapadmin:false, status:in progress, ...]]]]
        Possible solutions: parseText(java.lang.String), parse(java.io.Reader)
    at dap.Main.main(Main.groovy:171)

It works if i use the code as below

 def json = JsonOutput.toJson(statusresponse)
 def entry = slurper.parseText(json)

Am getting the results as below

The status of the action is :[in progress]
Last updated [2017-04-23 17:08:02.0]
the id is[170088]       

First of all, am not sure why the code suggested is throwing this error Secondly, why i am getting the results for the working solution, within the brackets?

3
  • Why do you want to round-trip to JSON again after the builder did all that for you? Commented Apr 24, 2017 at 13:06
  • Thanks, am able to resolve the issue Commented May 1, 2017 at 14:20
  • 1
    Than you should explain how you did! Commented Nov 30, 2017 at 11:02

1 Answer 1

2
def slurper = new groovy.json.JsonSlurper()
def entry = slurper.parseText('{"result":[{"id":"167687","dapadmin":"false","status":"in progress","lastupdated":"2017-04-21 14:34:30.0","started":"2017-04-21 14:34:28.0","user":"sys","log":"Running a Stop action\n\nRunning command \n"}]}')
def lastupdated = entry.result.lastupdated
def status = entry.result.status
def id = entry.result.id

Should work. Can't see your error so I can't give better info.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.