0

I have the following JSON String format getting from external source:- What kind of format is this actually?

{
id=102,
brand=Disha,
book=[{
   slr=EFTR,
   description=Grammer,
   data=TYR,
   rate=true,
   numberOfPages=345,
   maxAllowed=12,
   currentPage=345
   },
   {
   slr=EFRE,
   description=English,
   data=TYR,
   rate=true,
   numberOfPages=345,
   maxAllowed=12,
   currentPage=345
  }]
}

I want to convert this into actual JSON format like this: -

{
"id": "102",
"brand": "Disha",
"book": [{
    "slr": "EFTR",
    "description": "Grammer",
    "data": "TYR",
    "rate": true,
    "numberOfPages": 345,
    "maxAllowed": "12",
    "currentPage": 345
    },
    {
    "slr": "EFRE",
    "description": "English",
    "data": "TYR",
    "rate": true,
    "numberOfPages": 345,
    "maxAllowed": "12",
    "currentPage": 345
   }]
}

Is this achievable using groovy command or code?

avidCoder
  • 440
  • 2
  • 10
  • 28
  • Is that *really* the incoming string / text / information? Just to make sure that this is not something some program printed out and removed the `"` for whatever reason. – luk2302 Jan 02 '18 at 13:13
  • Yes, this is what I am getting in ReadyAPI tool while running using groovy code. I have not written any program that could remove the inverted commas from JSON. – avidCoder Jan 02 '18 at 13:15
  • @avidCoder, can you show the screen show of how you are getting the first message /string? – Rao Jan 02 '18 at 13:28
  • That format is jsonlite https://deerchao.net/projects/jsonlite/index.htm – ThomasEdwin Jan 02 '18 at 13:29
  • I have added the screenshot, for security purpose I have not added all the data. This is how it looks like. @Rao – avidCoder Jan 02 '18 at 13:35
  • @avidCoder, thank you. How many steps in your test case? Are you getting at as part of any previous step or something? If so, do you want to use that json for next request or so? Please clarify. – Rao Jan 02 '18 at 13:39
  • @Rao - There are 4 steps in the testCase. DataSource, Request, Groovy Script where I am writing the logics to get the json nodes and final one is DataSink where I am storing the json nodes. Yes I will going to use this json in other request. Basically I will be creating the request. So, here the format I am getting is not proper, How'd I put the same into new request? So, I want to convert this format into Actual json format and will pass this directly to the new request. – avidCoder Jan 02 '18 at 13:45
  • @avidCoder, Basically you want the response of step 2 as request for step4 (probably)? And there is no change of json? – Rao Jan 03 '18 at 01:18
  • Yes, correct. But the JSON format is not feasible. @Rao – avidCoder Jan 03 '18 at 05:20
  • @avidCoder, what do you mean by not feasible? – Rao Jan 03 '18 at 05:21
  • @Rao - oh sorry, I mean, I am not getting the correct JSON format in ready api tool. – avidCoder Jan 03 '18 at 05:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162387/discussion-between-rao-and-avidcoder). – Rao Jan 03 '18 at 05:24
  • Hey could you please help me add the quotes to the json key and values using regular expression. @Rao – avidCoder Jan 03 '18 at 06:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162390/discussion-between-avidcoder-and-rao). – avidCoder Jan 03 '18 at 06:20
  • @avidCoder, please check the updated answer under `EDIT` – Rao Jan 03 '18 at 08:48

2 Answers2

1

Couple of things:

  • You do not need Groovy Script test step which is currently there as step3
  • For step2, Add a 'Script Assertion` with given below script
  • Provide step name for nextStepName in the script below for which you want to add the request.
//Provide the test step name where you want to add the request
def nextStepName = 'step4'

def setRequestToStep = { stepName, requestContent ->
    context.testCase.testSteps[stepName]?.httpRequest.requestContent = requestContent   
}

//Check the response
assert context.response, 'Response is empty or null'
setRequestToStep(nextStepName, context.response)

EDIT: Based on the discussion with OP on the chat, OP want to update existing request of step4 for a key and its value as step2's response.

Using samples to demonstrate the change input and desired outputs.

Let us say, step2's response is:

{ 
    "world": "test1"
}

And step4's existing request is :

{
    "key" : "value",
    "key2" : "value2"
}

Now, OP wants to update value of key with first response in ste4's request, and desired is :

{
    "key": {
        "world": "test1"
    },
    "key2": "value2"
}

Here is the updated script, use it in Script Assertion for step 2:

//Change the key name if required; the step2 response is updated for this key of step4
def keyName = 'key'

//Change the name of test step to expected to be updated with new request
def nextStepName = 'step4'

//Check response
assert context.response, 'Response is null or empty'

def getJson = { str ->
    new groovy.json.JsonSlurper().parseText(str)
}

def getStringRequest = { json ->
    new groovy.json.JsonBuilder(json).toPrettyString()
}

def setRequestToStep = { stepName, requestContent, key ->
    def currentRequest = context.testCase.testSteps[stepName]?.httpRequest.requestContent
    log.info "Existing request of step ${stepName} is ${currentRequest}"
    def currentReqJson = getJson(currentRequest)
    currentReqJson."$key" = getJson(requestContent) 
    context.testCase.testSteps[stepName]?.httpRequest.requestContent = getStringRequest(currentReqJson)
    log.info "Updated request of step ${stepName} is ${getStringRequest(currentReqJson)}"   
}


setRequestToStep(nextStepName, context.request, keyName)
Rao
  • 20,781
  • 11
  • 57
  • 77
0

We can convert the invalid JSON format to valid JSON format using this line of code:-

 def validJSONString = JsonOutput.toJson(invalidJSONString).toString()
avidCoder
  • 440
  • 2
  • 10
  • 28