# Get data from request body in postman

**URL:** <https://community.postman.com/t/get-data-from-request-body-in-postman/76684>\
**Category:** Help Hub\
**Tags:** tests, variables\
**Created:** [March 10, 2025, 2:00pm UTC](https://community.postman.com/t/get-data-from-request-body-in-postman/76684 "2025-03-10T14:00:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![raniakanksha22](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@raniakanksha22](https://community.postman.com/u/raniakanksha22)\
**Post date:** [March 10, 2025, 2:00pm UTC](https://community.postman.com/t/get-data-from-request-body-in-postman/76684/1 "2025-03-10T14:00:17Z")

</div>

Hi  
My request body is :

```auto
query{
and[
{
        "gender": 1,
        "company": "MyCompany",
        "name": "Doe",
        "firstname": "John",
        "phone": "0606060606" 
},
{
 "gender": 1,
        "company": "MyCompany",
        "name": "Doe",
        "firstname": "John",
        "phone": "0606060606" 
}
]
},
"sort": {
"firstname": john,
"phone": "0606060606" 
}

}

```

`I want to access firstname value from query block .`  
Const body = JSON.parse( pm.request.body.raw);

Getting whole body in row.

**How to fetch specific value from request body .**

---

<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:** [March 10, 2025, 4:16pm UTC](https://community.postman.com/t/get-data-from-request-body-in-postman/76684/2 "2025-03-10T16:16:30Z")

</div>

You appear to have an array, with more than one record.

Which firstname do you want to retrieve?

That also doesn’t look like valid JSON! What is the query bit at the top? Is this a GraphQL request?

If you console log the body variable. Can you show us a screenshot of the result? (With all of the elements expanded out).

---

<div class="post-metadata">

**Author:** ![raniakanksha22](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@raniakanksha22](https://community.postman.com/u/raniakanksha22)\
**Post date:** [March 12, 2025, 1:45pm UTC](https://community.postman.com/t/get-data-from-request-body-in-postman/76684/3 "2025-03-12T13:45:04Z")

</div>

```auto
Const body = JSON.parse( pm.request.body.raw);
console.log(body);

```

```auto
Console result :-
{query: {...}, sort [1]...}

After expanding above got below 
query:{...}
sort: [1]

After expanding got this
query:{ ...}
and: [2]
0:{...}
1:{...}

After expanding 0

0: {...}
      gender": 1,
        "company": "MyCompany",
        "name": "Doe",
        "firstname": "John",
        "phone": "0606060606" 

1:{...}

```

---

<div class="post-metadata">

**Author:** ![raniakanksha22](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@raniakanksha22](https://community.postman.com/u/raniakanksha22)\
**Post date:** [March 12, 2025, 1:50pm UTC](https://community.postman.com/t/get-data-from-request-body-in-postman/76684/4 "2025-03-12T13:50:03Z")

</div>

I want to get “name” value from request body in my script . name or firstname from query block from request body .

---

<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:** [March 12, 2025, 2:55pm UTC](https://community.postman.com/t/get-data-from-request-body-in-postman/76684/5 "2025-03-12T14:55:44Z")

</div>

Try console logging the keys.

```auto
const body = JSON.parse(pm.request.body.raw);
console.log(body.query.sort[0].and[0].firstname)

```

If this doesn’t work then the console log the keys one at at time.

```auto
const body = JSON.parse(pm.request.body.raw);
console.log(body)
console.log(body.query)
// etc

```

It appears that your body returns an object with the top level key called “query”.

This has an key called “sort” with one value in it. You target array elements by their index number, where the indexes start at 0. So it will be sort[0].

Within the sort array, there is another array called “and” with two values in it.

Please note, you have more than one record with the same data. Is this correct? Which firstname do you need to target. and[0] or and[1].

Once you are passed this element, it looks like you should be able to target your firstname key.

```auto
console.log(body.query.sort[0].and[0].firstname)

```

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [March 25, 2025, 5:31am UTC](https://community.postman.com/t/get-data-from-request-body-in-postman/76684/6 "2025-03-25T05:31:17Z")

</div>

@raniakanksha22 , If you are new to APIs, handling and parsing the complex JSON responses might sound challenging. But you can master this if you learn more about the JSON structure. Since you pasted the  
response as image, its’ hard to locate the first name. Also seems like its an array.

You can paste your response in [https://jsonpathfinder.com/](https://jsonpathfinder.com/) and it gives you the xpath. that tool is pretty straightforward. If so, feel free to read my [blog](https://pricillabilavendran.com/2021/07/07/how-to-easily-locate-your-json-object-in-your-api-response/) which I wrote long back.

Good Luck 🙂
