# Hi Postman Community ? Can someone please help me with data driven testing. And to perform it using array

**URL:** <https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [January 24, 2023, 10:34am UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176 "2023-01-24T10:34:34Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![tushar37](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@tushar37](https://community.postman.com/u/tushar37)\
**Post date:** [January 24, 2023, 10:34am UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/1 "2023-01-24T10:34:34Z")

</div>

Need help on how to upload json file with array  
Getting 500 server error  
with error message " “data”: “Property ‘readings’ has value that is not of type ArrayNode (but com.fasterxml.jackson.databind.node.ObjectNode)”"

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 24, 2023, 11:29am UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/2 "2023-01-24T11:29:45Z")

</div>

You need to include sample code, including screenshots of the request, detailing which bits you need to come from the array, and an example of the array\JSON.

---

<div class="post-metadata">

**Author:** ![tushar37](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@tushar37](https://community.postman.com/u/tushar37)\
**Post date:** [January 24, 2023, 12:07pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/3 "2023-01-24T12:07:05Z")

</div>

Thanks for replying @michaelderekjones PFB

**Pre-request script** : - pm.variables.set(`requestBody`, JSON.stringify(pm.iterationData.toObject()))

**Body** : - {

}

 ![2023-01-24_17-37-48](https://us1.discourse-cdn.com/getpostman/original/3X/9/1/912c19082c1a21eb2b905d6a74e00e3b705b663f.png)

{  
“deviceId”: “8076334e-81d”,  
“readings”: {{requestBody}}  
}

**Variables** :-

 ![2023-01-24_17-34-47](https://us1.discourse-cdn.com/getpostman/original/3X/3/8/386227646b212f3b7af24335e06edc590668d639.png)

**JSON File** :  
 ![2023-01-24_17-35-45](https://us1.discourse-cdn.com/getpostman/original/3X/6/e/6e91fbcafe6b44f08629371f1583092c2bea35de.png)

Plaese have a look.

---

<div class="post-metadata">

**Author:** ![tushar37](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@tushar37](https://community.postman.com/u/tushar37)\
**Post date:** [January 24, 2023, 12:40pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/4 "2023-01-24T12:40:41Z")

</div>

![body](https://us1.discourse-cdn.com/getpostman/original/3X/2/7/273b26e272d7a634bf0bf612b818d466c6a2bc78.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 24, 2023, 12:40pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/5 "2023-01-24T12:40:48Z")

</div>

I would recommend using [https://jsonformatter.org/](https://jsonformatter.org/) or similar to minify the JSON, and then store it in a CSV file. Give the CSV file a header called “json” or “readings”.

When you run this through the Collection Runner, it technically has one record, so will run the request once.

In the body, remove the readings element (as we will add it in the pre-request script).

```auto
{
"deviceId": "8076334e-81d"
}

```

In your pre-request script, you import the string using the data tag and update the body at the same time.

```auto
var readings = JSON.parse(data.json);
const body = JSON.parse(pm.request.body.raw);
body.readings = readings;
pm.request.body.raw = body;

```

---

<div class="post-metadata">

**Author:** ![tushar37](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@tushar37](https://community.postman.com/u/tushar37)\
**Post date:** [January 24, 2023, 1:02pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/6 "2023-01-24T13:02:37Z")

</div>

@michaelderekjones do you want me to put

var readings = JSON.parse(data.json);  
const body = JSON.parse(pm.request.body.raw);  
body.readings = readings;  
pm.request.body.raw = body;

in pre-request as getting error

 ![pre](https://us1.discourse-cdn.com/getpostman/original/3X/d/e/de5aba34c16b7fbd323534fc109078a60651ab8a.png)

 ![2023-01-24_18-31-40](https://us1.discourse-cdn.com/getpostman/original/3X/1/f/1f81ee08fc6a858046159bdf8b59b7cdd90014f8.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 24, 2023, 1:10pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/7 "2023-01-24T13:10:18Z")

</div>

Remove the “readings” element from the body (and the comma at the end of the deviceId).

The pre-request script will add the readings element back in using the data from the CSV file.

You are running this through the collection runner?  
Using a CSV file with a single column with a header called “json” with your json array included?

You can also add the following to the end of the pre-request script to check what the updated body looks like.

```auto
console.log(pm.request.body.raw);

```

---

<div class="post-metadata">

**Author:** ![tushar37](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@tushar37](https://community.postman.com/u/tushar37)\
**Post date:** [January 24, 2023, 1:17pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/8 "2023-01-24T13:17:37Z")

</div>

Hey @michaelderekjones by any chance can we connect on meet to solve this ?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 24, 2023, 1:34pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/9 "2023-01-24T13:34:34Z")

</div>

Create a CSV file with a header.

The following is an example, which just contains the tag and value.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/3/a32675ef53f1d2f0be48d6e7371c3f2698a16e47.png)

Create a folder and add your POST request.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/1/013bd951f821248051c109abcc64a9baa4fb19a2.png)

My example is using Postman Echo.

The body just needs to include the deviceID element.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/3/c3ba5fe18b9315b4f3a3e4d65da87105bfab2ca8.png)

The pre-request script does the rest of the magic.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/f/ff51e2d9dc61ad6cb6723f172481b14ce252ebb1.png)

It retrieves the data from the CSV file in the first line.  
Then it gets the current request body.  
Then adds the readings element using the test data.  
Before updating the body which is what the request will actually use.

You run it through the Collection runner pointing it to the csv file.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/c/dc07087615dc5569332e934b085522e3c6171def.png)

This is the results of the console log, which shows the body that was used,

![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/0/b03f434708ad2e6c37856040dc9d998b0bb4957c.png)

---

<div class="post-metadata">

**Author:** ![tushar37](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@tushar37](https://community.postman.com/u/tushar37)\
**Post date:** [January 24, 2023, 2:50pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/10 "2023-01-24T14:50:12Z")

</div>

Thanks alot @michaelderekjones ! It works This time data get POST successfully 👍🏻

---

<div class="post-metadata">

**Author:** ![tushar37](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@tushar37](https://community.postman.com/u/tushar37)\
**Post date:** [January 27, 2023, 6:46am UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/11 "2023-01-27T06:46:53Z")

</div>

Hi @michaelderekjones I want to use timestamp from one response as request for other API through CSV.  
Can you please guide me through how can I directly store timestamp of API response in csv file and use as an request through runner. There are total 96 timestamp in a response body.  
PFB

 ![timestamp](https://us1.discourse-cdn.com/getpostman/original/3X/e/9/e987dc7602039987db77ca011416225a0ce5a04c.png)

 ![raw3](https://us1.discourse-cdn.com/getpostman/original/3X/2/e/2e8020c11dca583a0ae42deac6bb1686232d3ea2.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 27, 2023, 11:59am UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/12 "2023-01-27T11:59:44Z")

</div>

You don’t.

You get the response from your first call and then create an array of all of the timestamps using the JavaScript map function.

Once you have the array, you have two options.

You can have a loop in the Tests tab of the first request that uses sendRequest() to hit the new API with the details from the array.

Or you can create a second request and use the array.shift() method to loop though the array.

An couple examples of using array shift is here.

[https://community.postman.com/t/automate-post-request-with-different-variables-in-the-body-and-save-to-a-file/42937](https://community.postman.com/t/automate-post-request-with-different-variables-in-the-body-and-save-to-a-file/42937)

[https://community.postman.com/t/loop-through-values-from-get-to-next-put-request/41899/11](https://community.postman.com/t/loop-through-values-from-get-to-next-put-request/41899/11)

These are complex scenarios that you are trying to accomplish, and where I don’t mind giving to advice, you need to try these things out yourself and if you have issues, you can then ask more specific questions.

---

<div class="post-metadata">

**Author:** ![tushar37](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@tushar37](https://community.postman.com/u/tushar37)\
**Post date:** [January 27, 2023, 12:15pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/13 "2023-01-27T12:15:27Z")

</div>

@michaelderekjones can you please tell how to use JavaScript map function for above example.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 27, 2023, 12:24pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/14 "2023-01-27T12:24:20Z")

</div>

It’s a JavaScript function.

Please Google it first, and if you run into issues, show the code you’ve tried.

W3Schools is a good place to start as any.

Hint: Your reading element is an array, so this is what you need to target with the map.

The map function only works against arrays. If the element is not an array, it will error.

---

<div class="post-metadata">

**Author:** ![tushar37](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@tushar37](https://community.postman.com/u/tushar37)\
**Post date:** [January 27, 2023, 2:02pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/15 "2023-01-27T14:02:11Z")

</div>

I have successfuly segregate timestamp from API response and store in variable, can you guide me trhrough next step

 ![map](https://us1.discourse-cdn.com/getpostman/original/3X/b/f/bfd6a2d9d4fca6796696b0507757d39b7690eb56.png)  
 ![consol](https://us1.discourse-cdn.com/getpostman/original/3X/9/b/9bc0ef08d1e0ddb65093dda2c4b649e9529fbcb4.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 27, 2023, 4:14pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/16 "2023-01-27T16:14:49Z")

</div>

If you want to use the array.shift() method, then you need to store the new array as a collection or environment variable.

When storing arrays, please use JSON.stringify when storing, and JSON.parse when retrieving, otherwise it will just treat it as a string.

If you are going with the sendRequest() option, then you can work directly with the new array.

First step is to try out the array.

```auto
newValue.forEach(timestamp => {

   console.log(timestamp);
   
});

```

Once you have this working, you can then replace the console.log with the sendRequest().

An example using Postman Echo.

```auto
// ForEach loop with a sendRequest and tests.
array.forEach(timestamp => {
    pm.sendRequest({
        url: 'https://postman-echo.com/get?timestamp=' + timestamp,
        method: 'GET',            
    }, function (err, res) {
            if (err) {
                console.log(err);
            } else {
                // define the expectedResponse because we can also use this to format the test name.
                var expectedResult = timestamp;                
                // define the actual result, which will be the username from the response
                var actualResult = res.json().args.timestamp;                
                // Our actual test
                pm.test(`timestamp = ${expectedResult}`, () => {
                    pm.expect(actualResult).eql(expectedResult);
                });               
            }
    });
});

```

Read up on the sendRequest() function for more information on how to customise the request. For example how to define a json body to send.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 27, 2023, 5:01pm UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/17 "2023-01-27T17:01:20Z")

</div>

On a side note, there is a slightly easier way to create the array of timestamps.

```auto
const response = pm.response.json();

let timestamps = response.readings.map(obj => obj.timestamp);
console.log(timestamps);

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 13, 2024, 7:42am UTC](https://community.postman.com/t/hi-postman-community-can-someone-please-help-me-with-data-driven-testing-and-to-perform-it-using-array/43176/18 "2024-01-13T07:42:40Z")

</div>

Closed on January 13, 2024, 7:42am UTC


