4

I need to store the EventTypeId below in a global variable in Postman to be used on later tests:

{
    "EventTypeId": 8565382127936807869,
    "ValueName": "Engine Load Calculated",
    "FormatType": "Value",
    "DisplayUnits": "%",
    "EventType": "Custom",
    "Description": "Test Roja"
}

Because of the JS limit the value is stored as 8565382127936808000 and it fails when compared to the original one.

Is there a way to preserve it?

2 Answers2

1
str =  pm.response.text().replace(/[:|: *](\d+)/g,'"$1"');

console.info(str)

console.log(JSON.parse(str).EventTypeId)

pm.globals.set("value",JSON.parse(str).EventTypeId)

Here we are enclosing that number in double quotes and then parsing to json so that you get the correct value as string

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • Might need to adjust this snippets here to provide a way to save to a Global variable anf then a potential way to use the value is subsequent requests – Danny Dainton Feb 20 '21 at 08:28
  • @DannyDainton I think OPS is aware of that main concern was about the rounding anyways added that for future readers :D – PDHide Feb 20 '21 at 08:32
0

You could save the value as a string:

pm.globals.set('event_id', String(pm.response.json().EventTypeId)) 

When you need to use that in a script:

let eventTypeId = parseInt(pm.globals.get('event_id')
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80