0

Is there a way to retrieve all the values from a data source as a list in readyAPI ? if yes, how can i do it ? otherwise, any suggestion about how to populate a list in a request body from a json response of an API ?

Thanks in advance !

This is an example of the response,

`{
   "store" : {
      "book" : [
         {
            "id" : 1,
            "category" : "reference",
            "author" : "Nigel Rees",
            "title" : "Sayings of the Century",
            "price" : {
               "usd" : 8.95,
               "euro" : 8.95,
               "pound" : 7.95
            }
         },
         {
            "id" : 2,
            "category" : "fiction",
            "author" : "Evelyn Waugh",
            "title" : "Sword of Honour",
            "price" : {
               "usd" : 12.99,
               "euro" : 12.99,
               "pound" : 11.99
            }
         },
         {
            "id" : 3,
            "category" : "fiction",
            "author" : "Herman Melville",
            "title" : "Moby Dick",
            "isbn" : "0-553-21311-3",
            "price" : {
               "usd" : 8.99,
               "euro" : 8.99,
               "pound" : 7.99
            }
         }, 
      ],
   }
}`

I used data source but it retrieves only the last value,

i want all the books id to be populated as a liste in the booksId, this is my request body :

{"booksId":[1,2,3],
"reference":"test"}

1 Answers1

0

I don't see where the "reference":"test" in the output comes from, but the book IDs can be grabbed with json.store.book*.id.

The whole code sample:

String txt = '''\
{
   "store" : {
      "book" : [
         {
            "id" : 1,
            "category" : "reference",
            "author" : "Nigel Rees",
            "title" : "Sayings of the Century",
            "price" : {
               "usd" : 8.95,
               "euro" : 8.95,
               "pound" : 7.95
            }
         },
         {
            "id" : 2,
            "category" : "fiction",
            "author" : "Evelyn Waugh",
            "title" : "Sword of Honour",
            "price" : {
               "usd" : 12.99,
               "euro" : 12.99,
               "pound" : 11.99
            }
         },
         {
            "id" : 3,
            "category" : "fiction",
            "author" : "Herman Melville",
            "title" : "Moby Dick",
            "isbn" : "0-553-21311-3",
            "price" : {
               "usd" : 8.99,
               "euro" : 8.99,
               "pound" : 7.99
            }
         }
      ]
   }
}'''
def json = new groovy.json.JsonSlurper().parseText txt

def result = [ booksId:json.store.book*.id, reference:"test" ]

assert groovy.json.JsonOutput.toJson( result ) == '{"booksId":[1,2,3],"reference":"test"}'
injecteer
  • 20,038
  • 4
  • 45
  • 89
  • hello thanks for your answer ! I actually used the property transfert, so that i can transfert directly the list retrieved to the target field. – amlbenmansour Mar 29 '23 at 11:19