Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Null value converting string value to JSON

Kumar, G Sunil October 18, 2021

Hi Team,

 

 

When I am trying to convert the a string value to JSON usind JSON slurper I am getting null. Not sure why.

JiraNewTicketJSON =  {"fields":{"project":{"key":"TESTCM"},"issuetype":{"name":"Change Request"},"summary":"Testing","description":"Testing description",}}}}

finalBodyJson = JsonOutput.toJson(JiraNewTicketJSON)

jsonContent = new JsonSlurper().parseText(finalBodyJson)

finalJsonContent = groovy.json.JsonOutput.toJson(jsonContent.content)

 

I am getting 

finalBodyJson as {"fields":{"project":{"key":"TESTCM"},"issuetype":{"name":"Change Request"},"summary":"Testing","description":"Testing description",}}}}

but jsonContent is null

 

Regards,

Sunil

 

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
October 20, 2021

Hi @Kumar, G Sunil

You may want to modify your code slightly, as shown below:-

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

def JiraNewTicketJSON = "{'fields':{'project':{'key':'TESTCM'},'issuetype':{'name':'Change Request'},'summary':'Testing','description':'Testing description'}}}}"

def finalBodyJson = JsonOutput.toJson(JiraNewTicketJSON)

log.warn "====>> 1 ${finalBodyJson}"

def jsonContent = new JsonSlurper().parseText(finalBodyJson)

log.warn "====>> 2 ${jsonContent}"

def finalJsonContent = JsonOutput.toJson(jsonContent)

log.warn "====>> 3 ${finalJsonContent}"

Below is a print screen of this code and the test output:-

console_test.png

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram

Kumar, G Sunil October 20, 2021

@Ram Kumar Aravindakshan _Adaptavist_ ,

 

I tried the method but it still gives null value.

 

Regards,

Sunil

Kumar, G Sunil October 20, 2021

I am getting the log values but the ticket is not getting created and if I try to get the status code it says we cannot post on Null value

 

Regards,

Sunil

Kumar, G Sunil October 20, 2021

image.PNG

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
October 20, 2021

Hi @Kumar, G Sunil

Could you please share your full code? In the code that you have provided in your description, it is missing certain lines.

Thank you and Kind Regards,

Ram

Kumar, G Sunil October 21, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

 

The above method worked.

 

Thank you

Suggest an answer

Log in or Sign up to answer