# Extract value from a JSON Response Body via the Test Scripts

**URL:** <https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835>\
**Category:** Help Hub\
**Tags:** tests, variables, json\
**Created:** [April 8, 2020, 11:29pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835 "2020-04-08T23:29:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgreene7930](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jgreene7930](https://community.postman.com/u/jgreene7930)\
**Post date:** [April 8, 2020, 11:29pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/1 "2020-04-08T23:29:25Z")

</div>

I’m attempting to use a test script to store part of the response in an environment variable. Here’s the response that I’m attempting to parse:

```
{
    "meta": {
        "query_time": 1.61e-7,
        "trace_id": "bfa4ee6d-b15d-4920-8f9f-1a90bcb1b0b4"
    },
    "errors": [],
    "resources": [
        {
            "id": "xxxxxx",
            "group_type": "static",
            "name": "xxxxxx",
            "description": "xxxxxx",
            "assignment_rule": "xxxxx",
            "created_by": "xxxxx",
            "created_timestamp": "xxxxx",
            "modified_by": "xxxxx",
            "modified_timestamp": "xxxx"
        }
    ]
}

```

Here’s my test script code:

```
let responseData=pm.response.json();
console.log(responseData);
pm.environment.set("HostsToRemove", responseData.resources.name);

```

This fails with an error of “TypeError: Cannot read property of ‘name’ of undefined”. It appears to be because, under resources, there’s a set of brackets that seems to add another level to the JSON response. If i switch to responseData.meta.query\_time then my code works perfectly. I just can’t figure out how to modify the response data values to get down 1 more level so I can reference the ‘name’ field. Any ideas?

---

<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:** [April 9, 2020, 8:33am UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/2 "2020-04-09T08:33:23Z")

</div>

Hey @jgreene7930,

Welcome to the community! ⭐

Looking at your example response data - I can see that `resources` is an `array` which contains an `object`.

You would need to use `resources[0].name` to access the first object in the array.

You were on the right lines with logging the response to the Postman Console. This is what it would have logged:

![Screenshot 2020-04-09 at 09.27.10](https://us1.discourse-cdn.com/getpostman/original/3X/6/a/6a81a6fadf4bfbc2fadfadd07ae02b9bf6bf51b3.png)

This is showing you that `resources` is an array with 1 object and if you were to expand this, you will see that the objects are zero-indexed. The first object would be `0` and that’s the reference that’s been added above `[0]`.

So the full statement to set the variable would be:

```
let responseData = pm.response.json();
pm.environment.set("HostsToRemove", responseData.resources[0].name);

```

---

<div class="post-metadata">

**Author:** ![jgreene7930](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jgreene7930](https://community.postman.com/u/jgreene7930)\
**Post date:** [April 9, 2020, 2:45pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/3 "2020-04-09T14:45:12Z")

</div>

Thank you! That makes total sense. I appreciate your help here.

---

<div class="post-metadata">

**Author:** ![test.vtw](https://avatars.discourse-cdn.com/v4/letter/t/e8c25b/32.png) [@test.vtw](https://community.postman.com/u/test.vtw)\
**Post date:** [June 29, 2021, 10:19am UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/4 "2021-06-29T10:19:21Z")

</div>

Hi ,

I had a follow up question on this . My example response json is shown below  
{  
“cam:test”:“1970-01-01T00:00:00Z”,  
“cam:test2”:“1970-01-01T00:00:00Z”,  
“cam:test3”:“1970-01-01T00:00:00Z”,  
“cam:test4”:1970   
}

Could you please tell how do i retrieve the value when i have colon in the json field name

---

<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:** [June 29, 2021, 12:02pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/5 "2021-06-29T12:02:16Z")

</div>

Hi @test.vtw welcome to the community 🥳

Please read the similar post discussed [here](https://community.postman.com/t/how-to-read-the-data-from-json-response/21758).

You should be using the bracket notation instead of dot notation.

For example,

```auto
let response=pm.response.json();
console.log(response);
pm.environment.set("camtest1", response['cam:test']);
pm.environment.set("camtest2", response['cam:test2']);

```

This is just based on the response you have provided above, please edit the path if needed 😊 I hope this helps!

---

<div class="post-metadata">

**Author:** ![DIS\_Automation](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@DIS\_Automation](https://community.postman.com/u/DIS_Automation)\
**Post date:** [November 12, 2021, 9:09am UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/6 "2021-11-12T09:09:14Z")

</div>

Hi @bpricilla can you please help me extract and save the value of “id” inside ‘Body’ from below request  
[

```
{

    "Id": 1,

    "StatusCode": 201,

    "Headers": {

        "Trace-Id": "af40c0f6-cae1-45b6-b925-08f03b95b6a5",

        "Content-Type": "application/hal+json; charset=utf-8",

        "Location": "/districts/b4668e48-1039-4822-91df-81883c3579f4/ag-club-affiliations/5802631a-06d9-4901-86b7-8525a34a84a5",

        "Content-Length": "1138"

    },

    "Body": {

        "message": "",

        "startDate": "2021-12-01",

        "endDate": "2022-06-30",

        **"id": "5802631a-06d9-4901-86b7-8525a34a84a5",**

        "_links": {

            "self": {

                "href": "/districts/b4668e48-1039-4822-91df-81883c3579f4/ag-club-affiliations/5802631a-06d9-4901-86b7-8525a34a84a5",

                "method": "GET|PATCH|DELETE"

            },

            "ag_club_affiliations": {

                "href": "/districts/b4668e48-1039-4822-91df-81883c3579f4/ag-club-affiliations",

                "method": "POST|PUT"

            },

            "individual": {

                "href": "/individuals/189d8777-5dab-408d-a759-5e4696d59df4",

                "method": "GET|PATCH"

            },

            "district": {

                "href": "/districts/b4668e48-1039-4822-91df-81883c3579f4",

                "method": "GET"

            },

            "club": {

                "href": "/clubs/1063969e-341a-40a4-9713-02a54c3079f4",

                "method": "GET|PATCH"

            }

        },

        "_embedded": {

            "organization": {

                "name": "Los Ejidos-Piura",

                "id": "1063969e-341a-40a4-9713-02a54c3079f4",

                "type": "Rotary_Club",

                "_links": {

                    "self": {

                        "href": "/clubs/1063969e-341a-40a4-9713-02a54c3079f4",

                        "method": "GET|PATCH"

                    }

                }

            },

            "individual": {

                "id": "189d8777-5dab-408d-a759-5e4696d59df4",

                "firstName": "cxDHYu",

                "lastName": "Au",

                "middleName": "yyblxb",

                "prefix": null,

                "suffix": null,

                "localizedName": null,

                "_links": {

                    "self": {

                        "href": "/individuals/189d8777-5dab-408d-a759-5e4696d59df4",

                        "method": "GET|PATCH"

                    }

                }

            }

        }

    }

},

{

    "Id": 2,

    "StatusCode": 201,

    "Headers": {

        "Trace-Id": "af40c0f6-cae1-45b6-b925-08f03b95b6a5",

        "Content-Type": "application/hal+json; charset=utf-8",

        "Location": "/districts/911ee7b9-197f-4ac5-b752-5e803e3a79f4/ag-club-affiliations/4ad8d5e4-9828-436b-b35a-9c3ad24a84a5",

        "Content-Length": "1140"

    },

    "Body": {

        "message": "",

        "startDate": "2021-12-01",

        "endDate": "2022-06-30",

        "id": "4ad8d5e4-9828-436b-b35a-9c3ad24a84a5",

        "_links": {

            "self": {

                "href": "/districts/911ee7b9-197f-4ac5-b752-5e803e3a79f4/ag-club-affiliations/4ad8d5e4-9828-436b-b35a-9c3ad24a84a5",

                "method": "GET|PATCH|DELETE"

            },

            "ag_club_affiliations": {

                "href": "/districts/911ee7b9-197f-4ac5-b752-5e803e3a79f4/ag-club-affiliations",

                "method": "POST|PUT"

            },

            "individual": {

                "href": "/individuals/aeefcad1-ef32-4d4b-8ade-17e25d64acf4",

                "method": "GET|PATCH"

            },

            "district": {

                "href": "/districts/911ee7b9-197f-4ac5-b752-5e803e3a79f4",

                "method": "GET"

            },

            "club": {

                "href": "/clubs/13e87aad-592b-43fc-86f1-5e2c696279f4",

                "method": "GET|PATCH"

            }

        },

        "_embedded": {

            "organization": {

                "name": "Malikipuram",

                "id": "13e87aad-592b-43fc-86f1-5e2c696279f4",

                "type": "Rotary_Club",

                "_links": {

                    "self": {

                        "href": "/clubs/13e87aad-592b-43fc-86f1-5e2c696279f4",

                        "method": "GET|PATCH"

                    }

                }

            },

            "individual": {

                "id": "aeefcad1-ef32-4d4b-8ade-17e25d64acf4",

                "firstName": "IvWI",

                "lastName": "pWrH",

                "middleName": "AuDmGTy",

                "prefix": null,

                "suffix": null,

                "localizedName": "Nagendra",

                "_links": {

                    "self": {

                        "href": "/individuals/aeefcad1-ef32-4d4b-8ade-17e25d64acf4",

                        "method": "GET|PATCH"

                    }

                }

            }

        }

    }

},
```

---

<div class="post-metadata">

**Author:** ![Kevin\_Chang](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevin_chang/32/13426_2.png) [@Kevin\_Chang](https://community.postman.com/u/Kevin_Chang)\
**Post date:** [October 7, 2022, 6:36pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/8 "2022-10-07T18:36:05Z")

</div>

hey @DIS_Automation ~would you try this code?  
let response=pm.response.json();  
console.log(response);  
pm.environment.set(“id”,response.Body.id);

---

<div class="post-metadata">

**Author:** ![roman.dushko](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/roman.dushko/32/13931_2.png) [@roman.dushko](https://community.postman.com/u/roman.dushko)\
**Post date:** [November 16, 2022, 1:00pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/9 "2022-11-16T13:00:19Z")

</div>

Or there are might be a geeky long way to get it 🤭

```auto
const response = JSON.parse(Buffer.from(Array.from(pm.response.stream)).toString('UTF-8'))

```

before I learned `pm.response.json()` 😃

---

<div class="post-metadata">

**Author:** ![Craigs](https://avatars.discourse-cdn.com/v4/letter/c/73ab20/32.png) [@Craigs](https://community.postman.com/u/Craigs)\
**Post date:** [December 11, 2022, 1:55pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/10 "2022-12-11T13:55:54Z")

</div>

Very new to Postman,  
I’m trying to extract certain data from the response body and then use that data to send a post request this is where I’m at:

```auto
const response = pm.response.json();
const toolLtp = response.map(tool => tool.ltp)
console.log(toolLtp)

```

This is what my body is

```auto
{"status":"200","level":null,"message":"Success","data":{"ltp":1985.0,"Open":2000,"High":2110,"Low":1950}}

```

---

<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:** [December 12, 2022, 10:41am UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/11 "2022-12-12T10:41:58Z")

</div>

@Craigs

You don’t need to use map to get to the element you want.

```auto
let body = {
    "status":"200",
    "level":null,
    "message":"Success",
    "data":
    {
        "ltp":1985.0,
        "Open":2000,
        "High":2110,
        "Low":1950
    }
};

console.log(body.data.ltp); // 1985

```

---

<div class="post-metadata">

**Author:** ![Craigs](https://avatars.discourse-cdn.com/v4/letter/c/73ab20/32.png) [@Craigs](https://community.postman.com/u/Craigs)\
**Post date:** [December 14, 2022, 8:48am UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/12 "2022-12-14T08:48:39Z")

</div>

> [@michaelderekjones](#):
>
> ```auto
> let body = {
> "status":"200",
> "level":null,
> "message":"Success",
> "data":
> {
> "ltp":1985.0,
> "Open":2000,
> "High":2110,
> "Low":1950
> }
> };
> 
> ```

Thanks,  
The ltp, Open, High and low in the body here changes frequently it is not a fixed value so i need to send a get request and only extract the ltp from the body, how do i do that.

---

<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:** [December 14, 2022, 9:30am UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/13 "2022-12-14T09:30:47Z")

</div>

@Craigs

I’m assuming you mean the value changes frequently, but the “ltp” key should always appear?

As long as the key stays the same, then the example I provided should work.

```auto
console.log(body.data.ltp); // 1985

```

---

<div class="post-metadata">

**Author:** ![Craigs](https://avatars.discourse-cdn.com/v4/letter/c/73ab20/32.png) [@Craigs](https://community.postman.com/u/Craigs)\
**Post date:** [December 15, 2022, 6:14am UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/14 "2022-12-15T06:14:07Z")

</div>

Yes, the value of LTP changes frequently so this is what I’m doing:  
const response = pm.response.json();  
let body = {  
“status”:“200”,  
“level”:null,  
“message”:“Success”,  
“data”:  
{  
“ltp”:1985.0,  
“Open”:2000,  
“High”:2110,  
“Low”:1950  
}  
};  
console.log(body.data.ltp); // 1985  
so when i send a get request the value of ltp now has changed to 1990 but i still get 1985 i want to be able to get 1990 instead of 1985.

Thanks once again for helping me out here

---

<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:** [December 15, 2022, 2:19pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/15 "2022-12-15T14:19:29Z")

</div>

@Craigs

The “let body =” was just an example response I set for testing purposes.

Instead of parsing the response as I obviously don’t have access to your API. I just set a variable with the same format as the example body you provided.

It’s always going to return the figures that I hardcoded into the variable.

You would change this to the variable that you stored the parsed response.

```auto
const response = pm.response.json();
console.log(response.data.ltp);

```

If you are new to Postman. I would recommend going through the Postman training if you haven’t done so yet.

[Postman Galaxy Training | Postman API Network](https://www.postman.com/postman/workspace/postman-galaxy-training/overview)

Run through the “APIs 101 Training”, then the “Testing and Automation Training” course.

---

<div class="post-metadata">

**Author:** ![andrezmolina247](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/andrezmolina247/32/26593_2.png) [@andrezmolina247](https://community.postman.com/u/andrezmolina247)\
**Post date:** [November 21, 2023, 3:48pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/16 "2023-11-21T15:48:44Z")

</div>

Hi Everyone,

This is really useful, thank you for the contribution. I am dealing with a similar issue. I am trying to extract more than 1 value of the response and store them in different variables. (Ex: IP addresses). The test works if both values are the same(ex: enabled) but if I try with the IP it gives me the error “Couldn’t evaluate the test script:  
ReferenceError: address is not defined”  
Any idea of how could achieve that?

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

---

<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:** [November 21, 2023, 3:54pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/17 "2023-11-21T15:54:28Z")

</div>

Hey @andrezmolina247 👋

Welcome to the Postman Community! :postman:

What’s the full response body look like (mainly interested in the start of it)?

Is `objects` a actual property of the response or something that you have just added to the reference?

Does `responseData[0]["ipv4-address"]` work for you?

---

<div class="post-metadata">

**Author:** ![andrezmolina247](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/andrezmolina247/32/26593_2.png) [@andrezmolina247](https://community.postman.com/u/andrezmolina247)\
**Post date:** [November 21, 2023, 3:59pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/18 "2023-11-21T15:59:10Z")

</div>

The one that you sent me doesn not work. If I do  
pm.collectionVariables.set(“IP”, responseData.objects[0].enabled) it works

below the full response

```auto
{
    "objects": [
        {
            "comments": "",
            "enabled": true,
            "ipv4-address": "172.16.164.104",
            "ipv4-mask-length": "23",
            "ipv6-address": "Not-Configured",
            "ipv6-autoconfig": "Not configured",
            "ipv6-local-link-address": "Not Configured",
            "ipv6-mask-length": "Not-Configured",
            "name": "eth0",
            "type": "physical"
        },
        {
            "comments": "",
            "enabled": true,
            "ipv4-address": "7.7.7.2",
            "ipv4-mask-length": "30",
            "ipv6-address": "Not-Configured",
            "ipv6-autoconfig": "Not configured",
            "ipv6-local-link-address": "Not Configured",
            "ipv6-mask-length": "Not-Configured",
            "name": "eth1",
            "type": "physical"
        },
        {
            "comments": "",
            "enabled": true,
            "ipv4-address": "192.168.80.253",
            "ipv4-mask-length": "24",
            "ipv6-address": "Not-Configured",
            "ipv6-autoconfig": "Not configured",
            "ipv6-local-link-address": "Not Configured",
            "ipv6-mask-length": "Not-Configured",
            "name": "eth2",
            "type": "physical"
        },
        {
            "comments": "",
            "enabled": true,
            "ipv4-address": "127.0.0.1",
            "ipv4-mask-length": "8",
            "ipv6-address": "Not-Configured",
            "ipv6-autoconfig": "Not configured",
            "ipv6-local-link-address": "Not Configured",
            "ipv6-mask-length": "Not-Configured",
            "name": "lo",
            "type": "loopback"
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![andrezmolina247](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/andrezmolina247/32/26593_2.png) [@andrezmolina247](https://community.postman.com/u/andrezmolina247)\
**Post date:** [November 21, 2023, 4:07pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/19 "2023-11-21T16:07:58Z")

</div>

I just confirmed that the issue is related to the “-” character. If i try to do the same the with the value “name” where are different results it works. Still don’t know how to avoid that issue of the “-”

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

---

<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:** [November 21, 2023, 4:53pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/20 "2023-11-21T16:53:17Z")

</div>

That makes sense, it’s the reason I added `[0]["ipv4-address"]` in my response.

Certain characters used as property keys like `numbers`, `:` or `-` etc will break the chained reference to the property. They would need to be referenced using _bracket notation_ and not _dot notation_.

---

<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:** [April 11, 2024, 4:39pm UTC](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835/21 "2024-04-11T16:39:15Z")

</div>

A post was split to a new topic: [Comparing responses](https://community.postman.com/t/comparing-responses/59855)

[Next page](https://community.postman.com/t/extract-value-from-a-json-response-body-via-the-test-scripts/11835.md?page=2)
