Forum Discussion

oph_2022's avatar
oph_2022
Occasional Contributor
2 years ago

REST Request - JSON response without bracket

Hi,

I would like to know if it's possible to receive a Json response without the bracket ?

For example i would like this json response : 

[
{
"code": "0000000000",
"message": "OK"
},
{
"code": "0000000000",
"message": "OK"
},
{
"code": "0000000000",
"message": "OK"
}
]

 

Like this without the bracket [...] : 

{
"code": "0000000000",
"message": "OK"
},
{
"code": "0000000000",
"message": "OK"
},
{
"code": "0000000000",
"message": "OK"
}

 

If someone can share me a way to do this, it will be great.

I know the ndjson format can do this but i don't think this format is supported in ReadyAPI ?

 

Thanks,

Olivier

5 Replies

  • richie's avatar
    richie
    Community Hero

    Hey oph_2022,

    So your json response is contained within an array is that correct?

    If you're talking about a json response then you could us an event handler (TestRunListener.afterStep) to remove the array container, but this would actually alter the response to malformed json (if the example payload youve included in your post).

    The groovy would look something like the following:

    content = content.replaceAll("\\[", "")
    content = content.replaceAll("]", "")

    Cheers,

    Rich

    • oph_2022's avatar
      oph_2022
      Occasional Contributor

      Hey Ritchie,

       

      Thanks for your reply.

      My json response is not contained within an array.

      My JSON response is contained (Sequence Dispatch Style) here and i would like to know if i put the groovy code that you provied in your previous message can work here ? 

       

      Thanks, 

      Olivier,

       

       

       

      • richie's avatar
        richie
        Community Hero
        Hey oph_2022,

        Just to be clear, json arrays are represented by square brackets. All your payload content is contained within the square brackets, which means the whole content is saved within an array.

        Youre using virts for your testing, right? Sorry i didn't realise this from your initial message. I've only used readyapi virts a couple of times, but yeah you can tailor your response with a script, but i must be misunderstanding something essential here, cos i dont understand why youd need to alter the hardcoded response via an event handler as you can specify exactly the response you want, without the need for an event handler as a post-processor before you assert.

        Can you just explain your setup and use case a little more as i dont want to give you more incorrect info?

        As you control in thr virt exactly the response youre trying to generate, what happens when you remove the array container brackets (both the [ and ])??

        Cheers,

        Rich
  • oph_2022's avatar
    oph_2022
    Occasional Contributor

    Hey richie,

     

    For the context : 

    I need to sent a REST request and in return i'm waiting a response on Json format from my virtual service.

    My Json return have several objects and naturally, i need to put brackets "[" "]" in the beggining and at the end of the response like my example.

    And we must to provide a json response with several objects without the bracket.

    I think a groovy script must be used in this case to hide or remove the square bracket but i'm a newbie for that and i don't know how to do that.

    I hope my context is quite understandable.

    Kind Regards.

    • KarelHusa's avatar
      KarelHusa
      Champion Level 3

      oph_2022,

      I don't understand the issue since you can put anything in the virtual service response.

       

      But in any case, you have to respond with a correct JSON. Try to validate your JSON in https://jsonlint.com/ since ReadyAPI does not support JSON validation yet. 

       

      Best regards,

      Karel