# How to extract database values and set them as global variables

**URL:** <https://community.postman.com/t/how-to-extract-database-values-and-set-them-as-global-variables/42047>\
**Category:** Help Hub\
**Tags:** tests, variables\
**Created:** [December 14, 2022, 7:13am UTC](https://community.postman.com/t/how-to-extract-database-values-and-set-them-as-global-variables/42047 "2022-12-14T07:13:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lwj7](https://avatars.discourse-cdn.com/v4/letter/l/54ee81/32.png) [@lwj7](https://community.postman.com/u/lwj7)\
**Post date:** [December 14, 2022, 7:13am UTC](https://community.postman.com/t/how-to-extract-database-values-and-set-them-as-global-variables/42047/1 "2022-12-14T07:13:34Z")

</div>

Hello, as the title shows, we have successfully accessed MySQL through the postman, requested the database in the interface, and returned the response result. But the value extracted by my previous method of extracting response results is null. How should we correctly extract the response result field value of the database interface? Thank you very much for your advice

Previously, the successful extraction of response results is achieved by:

var Jsondata = JSON.parse(responseBody);

pm.globals. set(“abc”, Jsondata.id);

---

<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, 12:22pm UTC](https://community.postman.com/t/how-to-extract-database-values-and-set-them-as-global-variables/42047/2 "2022-12-14T12:22:25Z")

</div>

Can you please share a screenshot of an example response (with any confidential information redacted).

This isn’t what is causing your problem, but the JSON.parse method you are using was the old way of parsing JSON responses.

You can now do this through the pm.repsponse object.

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

```

[Postman JavaScript reference | scripting-with-response-data](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#scripting-with-response-data)

---

<div class="post-metadata">

**Author:** ![lwj7](https://avatars.discourse-cdn.com/v4/letter/l/54ee81/32.png) [@lwj7](https://community.postman.com/u/lwj7)\
**Post date:** [December 15, 2022, 1:26am UTC](https://community.postman.com/t/how-to-extract-database-values-and-set-them-as-global-variables/42047/3 "2022-12-15T01:26:55Z")

</div>

(post deleted by author)

---

<div class="post-metadata">

**Author:** ![lwj7](https://avatars.discourse-cdn.com/v4/letter/l/54ee81/32.png) [@lwj7](https://community.postman.com/u/lwj7)\
**Post date:** [December 15, 2022, 1:28am UTC](https://community.postman.com/t/how-to-extract-database-values-and-set-them-as-global-variables/42047/4 "2022-12-15T01:28:21Z")

</div>

Thank you for your answer. The method you provided has been used to perform the operation. The response data has been screenshot. How should I extract the ID value in the response result and set it as a global variable

 ![微信图片_20221215092346](https://us1.discourse-cdn.com/getpostman/original/3X/8/0/8078327b0752675acbe83a3a15a7538be36b30e0.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:** [December 15, 2022, 11:59am UTC](https://community.postman.com/t/how-to-extract-database-values-and-set-them-as-global-variables/42047/6 "2022-12-15T11:59:31Z")

</div>

Why a global variable? This means it will be available to all collections.

The scope should probably be set to collection.

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

Your response is an array of Json objects (it’s also a Json object in its own right).

the pm.response.json() parses the JSON into a JavaScript object.

You will need the first element in the array which is [0], and the ID element within.

This should do the trick.

```auto
const response = pm.response.json();
let id = response[0].id
pm.collectionVariables.set("variable_name", id);

```

---

<div class="post-metadata">

**Author:** ![lwj7](https://avatars.discourse-cdn.com/v4/letter/l/54ee81/32.png) [@lwj7](https://community.postman.com/u/lwj7)\
**Post date:** [December 16, 2022, 2:46am UTC](https://community.postman.com/t/how-to-extract-database-values-and-set-them-as-global-variables/42047/7 "2022-12-16T02:46:38Z")

</div>

Thank you for your answer.  
Yes, I want to use it for all collections, because I have interfaces for other collections. According to your method, the problem has been solved.  
I would like to express my gratitude again. Thank you
