Building a dynamic JSON Request body with n objects in an array

I have a use case that I'm trying to implement in SOATest and am running into a wall.

I have a Service1 that returns n number of Account objects. There are a varying amount of accounts that could be returned for a given request as that user may have more or less accounts than another.

I have a second service, Service2, that takes a structured request of n Account objects and AccountId is an integer that is required on all Accounts passed.

What I am attempting to do is extract AccountIds from the Service1 response and then build out a request to Service2 in a dynamic fashion. I have tried using a JSON Databank to extract the AccountIds from Service1 and then paramaterized that to the Service2 request but it just pastes ALL the AccountIds into a single AccountId field. Is there any way to dynamically create an array of objects using SOATest, in memory?

Comments

There are possibly more elegant implementations, but off the top of my head I would use an XML data bank to extract all of the element values to a Writable data source.

Modify the Xpath to grab all values not just a single one.

Once written into a data source you can then use the
context.getValues("writable_name","column_name") method to move the values to an arraylist and then simply loop the values while adding the tags. You would then have your xml body.

This Example requires the use of a test suite variable. (Double click the test suite and navigate to then variable tab)
Example script:import com.parasoft.api.*;

It is common for to take elements/objects/arrays from a response and then reuse them in a subsequent request. Typically, you would do the following:

Extract the desired element/object/array that you want to reuse to a data bank column. In the data bank, click Modify to open the Modify Selected XPath settings dialog. Make sure "Entire element" is selected and not "Content only".

In the subsequent request, replace the desired element/property/item with the data bank column. In the Form view, you would right-click the on the element you want to replace then click "Replace with Data Source Value", select Data Bank, choose your data bank column, then click Finish.

What I mention applies to both JSON and XML. For JSON, you would be using a JSON Data Bank and the Form JSON view. For XML, you would be using an XML Data Bank and the Form Input view.

Sometimes using the extraction as-is is desired. However, in some scenarios you want to transform the data in some way before reusing it. This could be accomplished using scripting (Extension Tool) or using XSL (XSLT Tool). Some simple transformations, like removing or appending values, can be applied by Data Bank itself by enabling the "Allow alteration" box under Options (collapsed by default).

Ramiro's response is one such example where scripting can be used to transform extracted values.

Here is a sample of the response that I am extracting the AccountId values from:

From this response I have the following data bank/xpath setup. As you can see it's set in "AccountId, ####" format. Which I would expect to be able to use.

In the secondary request I am trying to build here is a sample of the form JSON (There will be other elements, I'm just trying to get this one working for now as a POC).

I have tried setting the Replacement at the Accounts, and Item[0] level (as those are the only two that allow it. I cannot add a replacement at the AccountId level). I receive the following error when attempting this.

Please advise if I am missing something but as far as I can tell SOATest does not natively support the use case for which I am trying. Short of coding up an extension to build this request I cannot see a path forward.

The steps I described are for extracting the entire array and then replacing the entire array element. If you want to do that then you would extract the value of the AccountSummaryList array and then replace the Accounts array in the subsequent test.

As mentioned, more steps or different steps would be required if you need to transform the contents of the array being extracted (change/add/remove values or rename things). As mentioned, this may have involve scripting or XSL. Ramiro gives one such example. Please note that if you follow Ramiro's example, he's not extracting the entire array but the contents to a Writable Data Source and using scripting to construct the desired result (which is a different approach than what I describe but also fine).