Forum Discussion

viswam_i's avatar
viswam_i
New Contributor
8 years ago

How to capture values from JSON request and pass to response

All,

I am working on REST service mocking. As part of this need to capture JSON request and from there capture few values  and pass to response. Can anyone please help me how to do this ?

 

Thanks

Vis

2 Replies

  • Hi

     

    have a look at this http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html

     

    you will probably use something like this to get your json payload

     

    def slurper = new JsonSlurper()
    def result = slurper.parseText(mockRequest.getRequestContent())

     

     

    use an example from the link above to see how to get specific values

     

    within your dispatch script set a property value to hold the respective json element value

     

    context.setProperty( "PropertyName", JSONElementValue)

    then within your response simply use ${PropertyName} where you want your inbound value to be returned

    i.e.

    { "Request value" : "${PropertyName}" }

    obviously using a naming convention that suits

     

    hope that helps

     

     

  • nmrao's avatar
    nmrao
    Champion Level 3
    It would be good if you can show the data that you are working with and what data do you need to extract.