# Send Array Index Value in JSON Request Body for Batch Operation using Runner

**URL:** <https://community.postman.com/t/send-array-index-value-in-json-request-body-for-batch-operation-using-runner/48857>\
**Category:** Help Hub\
**Tags:** array, collection-runner\
**Created:** [June 23, 2023, 5:58am UTC](https://community.postman.com/t/send-array-index-value-in-json-request-body-for-batch-operation-using-runner/48857 "2023-06-23T05:58:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![0sanjary](https://avatars.discourse-cdn.com/v4/letter/0/f05b48/32.png) [@0sanjary](https://community.postman.com/u/0sanjary)\
**Post date:** [June 23, 2023, 5:58am UTC](https://community.postman.com/t/send-array-index-value-in-json-request-body-for-batch-operation-using-runner/48857/1 "2023-06-23T05:58:02Z")

</div>

Here’s my problem scenario:

I have an array of 10K user\_id. I need to send them in Request Body using Collection Runner. I need to send 100 user\_ids in one request and I will loop through the request 100 times using Collection Runner.

**RequestBody**

```auto
"root": [
    {
        "id": "{{currentId[0]}}",
        "type": "user",
    },
    {
        "id": "{{currentId[1]}}",
        "type": "user",
    },
    {
        "id": "{{currentId[2]}}",
        "type": "user",
    },
    .
    .
    97 times more
    .
    .
],

```

**Pre-Request Script:**

```auto
const actualIdList = [1,2,3,4,....10000];
let idList = pm.collectionVariables.get("idList");

if(!idList || idList.length == 0) {
    idList = actualIdList;
}

let currentId = []
if(idList.length >= 10) {
    for(let i = 0; i < 10; i++) {
        currentId[i] = idList.shift();
        pm.collectionVariables.set(`currentId[${i}]`, currentId[i]);
    }
}

pm.collectionVariables.set("idList", idList);

```

**Tests:**

```auto
const idList = pm.collectionVariables.get("idList");

if (idList && idList.length > 0){
    postman.setNextRequest("Sending for 100 User Ids");
} else {
    postman.setNextRequest(null);
}

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

```

But using this syntax `{{currentId[1]}}` fails to parse the actual value when the raw request is posted to the API endpoint.

How to resolve this? Need your guidance. Thank you.  
(I also see a similar question posted [here](https://stackoverflow.com/questions/52751695/how-to-send-array-index-value-into-json-body-in-postman))

---

<div class="post-metadata">

**Author:** ![0sanjary](https://avatars.discourse-cdn.com/v4/letter/0/f05b48/32.png) [@0sanjary](https://community.postman.com/u/0sanjary)\
**Post date:** [June 23, 2023, 6:32am UTC](https://community.postman.com/t/send-array-index-value-in-json-request-body-for-batch-operation-using-runner/48857/2 "2023-06-23T06:32:45Z")

</div>

The only solution that comes to my mind right now, is following but I dislike it.  
(Typing out variable names in sequential order)

**RequestBody**

```auto
"root": [
    {
        "id": "{{currentId_0}}",
        "type": "user",
    },
    {
        "id": "{{currentId_1}}",
        "type": "user",
    },
    {
        "id": "{{currentId_2}}",
        "type": "user",
    },
    .
    .
    do this 97 times more
    .
    .
],

```

**Pre-Request Script:**

```auto
//Everything same as the above Pre-Request Script:

let currentId = []
if(idList.length >= 10) {
    for(let i = 0; i < 10; i++) {
        currentId[i] = idList.shift();
        pm.collectionVariables.set(`currentId_${i}`, currentId[i]);
    }
}

```

---

<div class="post-metadata">

**Author:** ![0sanjary](https://avatars.discourse-cdn.com/v4/letter/0/f05b48/32.png) [@0sanjary](https://community.postman.com/u/0sanjary)\
**Post date:** [June 23, 2023, 7:05am UTC](https://community.postman.com/t/send-array-index-value-in-json-request-body-for-batch-operation-using-runner/48857/3 "2023-06-23T07:05:13Z")

</div>

I just went with Python Scripting which resolved this issue for me.  
Still any solution is welcome. Thanks

---

<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:** [June 23, 2023, 9:37am UTC](https://community.postman.com/t/send-array-index-value-in-json-request-body-for-batch-operation-using-runner/48857/4 "2023-06-23T09:37:28Z")

</div>

> [@0sanjary](#):
>
> `pm.collectionVariables.set(`currentId[${i}]`, currentId[i]);`

Your collection variables will be named.

currentId1, currentId2, etc.

So that is how they need to be referenced in the body.

{{currentId1}} not {{currentId[0]}

You do have an array called currentId, but you can’t reference them in the body using the handlebars (curly brackets) due to scope. The array is a local variable which will only be available to the pre-request script. It won’t be available to the body so they will be returned as “undefined”. (You can’t reference local or global variables in the body via its handlebars).

I also recommend you call the variable something else, so its clear on what is what.

[Using variables | Postman Learning Center](https://learning.postman.com/docs/sending-requests/variables/)

Another option here is to have place holders in the body for these ID’s, and then directly overwrite them using the following method. This way the body will have the actual value and you are not replying on variable per se at this point.

```auto
const body = JSON.parse(pm.request.body.raw); // retrieve current body
// update the elements using your loop
// you should be able to target the element using the array index
// using the same index number that you are looping through your test data
pm.request.body.raw = body; // write back updated body

```
