1

I have a problem where jsonBuilder strips quotes from the result string. How do I format the output to return a JSON response with quotes ?

import com.eviware.soapui.support.XmlHolder
import net.sf.*
import net.sf.json.*
import net.sf.json.groovy.*
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import groovy.json.*
import groovy.json.JsonOutput
import net.sf.json.JSONObject

def ResponseMessage = testRunner.testCase.testSteps["MerchantEMS_POST"].testRequest.response.contentAsString
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
log.info ResponseMessage

def merchantResult = ResponseMessage
def newMerchantID = "60300004055" 
def entityID = jsonSlurper.entityId
jsonSlurper.merchantId   = newMerchantID
def jsonBuilder = new groovy.json.JsonBuilder()
def updatedjson = jsonBuilder(jsonSlurper)
log.info "updated JSON = $updatedjson"

return updatedjson

ResponseMessage : { "entityId" : "93LSHLXW7BJ5K00MJALWZJMLL0", "creatorId" : "HPCDKMSV763K2VGHCKQQ09QSGM", "createdTimestamp" : "2015-09-02T00:26:34.015Z", "updaterId" : "HPCDKMSV763K2VGHCKQQ09QSGM", "updatedTimestamp" : "2015-09-02T00:26:34.015Z", "merchantId" : "L7QWKA0001F5W1RRZY4Z006153", "createdBy" : "ralgg00", "isDeleted" : false }

updatedjson (no quotes) = [updatedTimestamp:2015-09-02T00:26:34.015Z, createdBy:ralgg00, createdTimestamp:2015-09-02T00:26:34.015Z, creatorId:HPCDKMSV763K2VGHCKQQ09QSGM, entityId:93LSHLXW7BJ5K00MJALWZJMLL0, merchantId:60300004055, isDeleted:false, updaterId:HPCDKMSV763K2VGHCKQQ09QSGM]

1 Answers1

1

EDIT:

When you log the 'updatedjson' it recognises it as Map object and prints its fields. You need to use something that can convert a Map object to JSON and print it out. There are many ways to do this, for example:

 def json = JsonOutput.toJson(updatedjson)
 println json

Source: http://www.groovy-lang.org/json.html

Aston
  • 3,654
  • 1
  • 21
  • 18
  • Thanks Aston. I still have the same problem after trying the following additional code with SOAPUI 5.1.2: def updateJsonString = updatedjson.toString() log.info "updateJsonString = $updateJsonString" def strgFormat = new StringWriter() strgFormat << updateJsonString log.info "strgFormat = $strgFormat" – Richard Alagna Sep 03 '15 at 21:09
  • Ok, now I got a solution for you, see the updated answer. – Aston Sep 04 '15 at 06:35
  • 1
    Hi Aston. That works (JSON-formatted string out) once I ADD "import groovy.json.JsonOutput" with def json = JsonOutput.toJson(updatedjson) !!! Thanks again. – Richard Alagna Sep 04 '15 at 19:40