pub.json:getNextBatch
WmPublic. Gets the next batch of array elements by parsing the array paths in the iterator object returned by the pub.json:getArrayIterator service. This service returns the array elements in batches based on the batch size provided in the input. The batch size can vary across invocations of this service. A batch is a set of elements that can be retrieved from an array path at once, based on the batch size. To retrieve the remaining elements in the array path or elements from the next array path in the iterator, invoke the service in a loop until there are no more array paths to iterate.
Input Parameters
iterator | Object. The iterator object returned by the pub.json:getArrayIterator service. |
batchSize | Object. Number of array elements that the service should retrieve in one batch. Note: This value must be lesser or equal to the value of the watt.server.json.iterator.maxBatchSize server configuration property. Similarly, it must be greater or equal to the value of the watt.server.json.iterator.minBatchSize server configuration property. Otherwise, the service throws an error. |
Output Parameters
batch | Document. IData object that contains the following keys. |
| Key | Description |
| arrayPath | String. The path of the array elements retrieved in a batch. Since the service can iterate over multiple arrays, each batch contains the array path parsed in current iteration to help you identify the array to which the elements in the batch belong. |
| documents | Document List. If array elements in the batch are JSON objects, they are returned as documents. |
| values | Object List. If array elements in the batch are not JSON objects, they are returned as values. |
iterationStatus | Document. IData object that contains the following keys. |
| hasNext | String. Indicates whether there are more array elements in the iterator beyond this batch, which the service can retrieve. The value can be true or false. |
| iteration | String. Indicates the current iteration. It starts from 1. If the service runs N times to get all the array elements, then this value is N. |
| numberOfElementsInBatch | String. Indicates the number of elements in the current batch. This value is same as batchSize. However, this value can be less than the batch size for the last batch. For example, if there are 7 elements in an array and the batch size is 5, then the last batch will have only 2 elements. |
| totalElementsParsed | String. Indicates the number of array elements parsed until the current iteration. For example, if the number of elements parsed in the first iteration is 10, second iteration is 20, and third iteration is 7, then in the third iteration, the value of this parameter is 37. |
Usage Notes
The pub.json:getNextBatch service completes the retrieval of elements from one array in the iterator and starts retrieving from the next array that matches in the subsequent iteration. This is explained in the following example:
Suppose you want the service to parse a JSON file with 2 arrays, for example, A and B with 6 and 2 elements each. Set the
batchSize input parameter to 5 and invoke the service in a loop until the service returns the
hasNext parameter as
false. In the first batch of the output, the first 5 elements of the array A are returned and in the next batch, only the last element of the array A is returned, even though the batch size is 5. At this point, the
hasNext parameter is
true because array B is not parsed yet. In the next batch, the service returns both the elements of array B. Since there are no more elements left either in the array A or B, the
hasNext parameter becomes
false. For a detailed use case, see this
article.
Guidelines
If two array paths overlap, the path that the service finds first in the input JSON stream is parsed and the other path is ignored. If you provide
arrayPath[0] as
/a/b and
arrayPath[1] as
/a/b/0/c to parse the following array,
arrayPath[1] is ignored as the paths overlap and the first path retrieves all elements.
{
"a": {
"b": [{
"f1": "v1",
"c": [1, 2, 3]
}]
}
}
If an invalid array path is provided in the input, the
pub.json:getNextBatch service does not return any result on parsing the array.
If the
arrayPaths input parameter is not set, then the
pub.json:getNextBatch service parses the input considering that the input stream has a single anonymous array at the root level.
If the
arrayPaths input parameter is set, then it cannot contain null or empty elements.
If one or more array paths are invalid, then the
pub.json:getArrayIterator service still creates an iterator object containing these paths. However, the
pub.json:getNextBatch service ignores the invalid paths.
If all the paths are invalid, the pub.json:getNextBatch service returns the documents and values parameters as null, the hasNext parameter as false, and all other output parameters as 0.