How to store a JSON in a variable and use it in the request body?

@singhsivcan @cdxcz Similarly I have one requirement, would like to understand who I move ahead with Postman.
1.Select the GET Method.
2. Enter the End point http://dummy.restapiexample.com/api/v1/employees URL.
3. Click on send button and get the response.
4. Observe the response.
5. Verify the response body through test script and data driven approach(.csv file)

Who I can verify those data through script, Yes I know one by one I can verify it.
but wanted to make script more generic.
Like put some data into variable, Put same variable and values into .csv file, run the collection run and verify the response.

@aakashjain8693 You cannot read a CSV file from the test script unfortunately within the Postman app.
You might need to use newman to achieve something like this which is our CLI tool and also can be integrated in node.

I am sure @danny-dainton has some ideas around a solution for this kind of problem. :smile:

@singhsivcan Thanks for the response, Lets wait for @danny-dainton .

Hi @singhsivcan,

I am unable to work with variable, Variable showing null when used {{temp_token}}

var tempvar = console.log(pm.response.json()["temp token"]); 

temp_token = JSON.stringify(tempvar); 

pm.environment.set("temp_token", temp_token); 

@danny-dainton your help would be great

Hey @gpub1

You might to also need show the response body and how youā€™re using that variable in the app.

Itā€™s difficult to know what youā€™re seeing with just the script youā€™re using to extract the value :grin:

1 Like

:slight_smile: yes, kind a new guy on postmanā€¦

{
    "temp token": "45351467"
}

This is the response body :smiley:

Your response body looks quite basic, donā€™t see many keys with spaces but that reference should work.

Before running, ensure that you have created and selected an environment file.

let tempVar = pm.response.json()["temp token"]; 

pm.environment.set("temp_token", tempVar); 

Add that to the Tests script of the request, the tempVar should then be available for you to use.

1 Like

I believe the behaviour youā€™re observing is explained here: javascript - Why does console.log say undefined, and then the correct value? - Stack Overflow

Thanks Dannyā€¦ Its working nowā€¦ and skipped this line temp_token = JSON.stringify(tempvar);

1 Like

Wasnā€™t really sure why you even had it in there :thinking:

Glad you got it working. :trophy:

1 Like

Any update @danny-dainton, how I can achieve with Nested array or objects?

@allenheltondev looped you to find out the best result for us.

Since we seem to be hijacking the thread, it might be better to start a new post with your screenshots and use case stated again.

I think weā€™ll get better traction that way.

@allenheltondev WIll do, Will give all required details.

1 Like

What if I have a payload like this set in the globals file:
[{ā€œxā€:ā€œabcā€,ā€œyā€:ā€œefgā€,ā€œzā€:ā€œhijā€}]

But letā€™s say I want to only access ā€œyā€ value inside a JSON RAW Body?
Is that possible? I looked at solutions to escape the ā€œā€ but Iā€™m struggling.

In this case, I would use a prerequest script to scope down the variable you want.

const globalValues = JSON.parse(pm.globals.get('myPayload'));
pm.variables.set('body', globalValues.y);

In the body of your request, you can then reference this value simply by adding in {{body}}

1 Like

Hi @singhsivcan or other enthusiasts,

Iā€™ve been reading up for the past 3 days and trying out all sorts of suggestions and pieces of code shared here but canā€™t seem to get things to work as I want. The last I tried was @Sandokhan 's example of ā€œcitiesā€.

I would appreciate anyoneā€™s feedback here.

I am requesting the azure devops service to get a list of projects in our organization and want to re-use the project names in the response in a next request where I need the names to get a another list of what really want to have.

Example response output:

{
    "count": 123,
    "value": [
        {
            "id": "11erui332-1323-4ed3-23r123d4",
            "name": "PROJECT-NAME",
            "url": "https://dev.azure.com/myorg/_apis/projects/id-of-project",
            "state": "wellFormed",
            "revision": 321,
            "visibility": "private",
            "lastUpdateTime": "2020-05-19T14:04:12.69Z"
        },

The ā€œnameā€ value is what I am after. I would like to get all the names and use them in the next request, inserted in the {{project}} variable:

https://dev.azure.com/{{organization}}/{{project}}/_apis/distributedtask/queues?api-version=6.0

pm.collectionVariables.set() and pm.collectionVariables.get() seem the way to go, but Iā€™m missing something Iā€™m sure.

If thereā€™s an example out there Iā€™ve missed let me know please.

Thanks a lot for any pointers, Iā€™m totally new at this. I did manage to get out nice lists and formatted them in tabular output with the visualization, very nice.

Hey @mschuijer, since I can see that youā€™ve an array of value in the response, and you only want the first name for your next request?

I think what you want is the following test script (make sure itā€™s NOT put under the pre-request script):

let projectName = _.get(pm.response.json(), 'value.0.name');

pm.collectionVariables.set('project', projectName);

Now when you run the next request then it should have the correct project name resolved from the collection variable.

Thank you for the response.
I actually want to save all the (135) project names and use each one in the next request:
https://dev.azure.com/{{organization}}/{{project}}/_apis/distributedtask/queues?api-version=6.0-preview.1

I donā€™t know if thatā€™s possible though? The request has only a place for one {{project}} (name) and I wouldnā€™t know how this would be able to create on output result for all project names.

Thanks again

Hi Sivcan, Experts,

I have been trying to develop a piece of code to store the post request output(in json format) in an array and compare the array output with a value to see if it matches my condition.

Requirement:
POST request on https://frxxxxxxxxxxxxxxxxxxxs.cxxxxxxxCOxxxxxxxxxNG
Bearer Token

parse the answer

{
ā€œCE:get_versionā€: {
ā€œreturn_codeā€: 0,
ā€œmessageā€: ā€œDONEā€,
ā€œelapseā€: 78
},
ā€œCE:get_statusā€: {
ā€œreturn_codeā€: 0,
ā€œmessageā€: ā€œDONEā€,
ā€œelapseā€: 390
}
}
check that all ā€œreturn_codeā€ entries are set to 0.
Beware that the number of entries is dynamic and can vary in time, do not assume a fixed content (e.g below answer shows two entries CE:get_version and CE:get_status, but the project will incrementally add some with new names over time without altering the monitoring scenario).

In case any of the return_code is != 0, trigger an error message that includes the full json answer.

Could you please suggest on this. Thanks in advance.