From string to int

I am attempting to retrieve an int from a json array, then use that as a variable in my request body.

This is what i have so far:

const jsonData = pm.response.json();

const tempId = jsonData['Content']['2020-06-12']['15'][8]['Id'];

parseInt(pm.environment.get('test-tempId', tempId));

[id] is an int i.e 123456

Any help appreciated.

This doesn’t look quite right to me.

To set the value you would just do this:

pm.environment.set('test-tempId', tempId);

To parse that stored variable string value to an Int, you would need to get it like this:

parseInt(pm.environment.get('tempId'));

in the following what does pm stand for

pm.environment.set('test-tempId', tempId);

How is the response called i.e the call that gets the data is called Request

this is the Test script now

const jsonData = pm.response.json();

const tempId = jsonData['Content']['2020-06-12']['15'][8]['Id'];
parseInt(pm.environment.get('tempId'));
pm.environment.set('test-tempId', tempId);

The pm.* API is a set of global functions that can be used with the sandbox environment.

In your case, you are using pm.environment but you could use pm.globals or pm.collectionVariables depending on what you’re trying to achieve.

These are just a few of the functions that are available but the full list can be found here:

https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/

For your issue - This code, in a Test Script, would be getting the full JSON response body and then extracting the Id value from that part of the data and storing it as an environment variable.

const jsonData = pm.response.json();

const tempId = jsonData['Content']['2020-06-12']['15'][8]['Id'];
pm.environment.set('test-tempId', tempId);

You could just use that stored variable, in a request body like this:

{
    "test-id": {{test-tempId}}
}

If you add quotes around the "{{...}}" it would send that as a string but as you haven’t it should just send that as an Int