skip to Main Content

I have a problem when during a performance test I get a response that contains items like

{"item":{"id":2733382000000000049}}

The k6 response.json() parses it as :

{"item":{"id":2733382000000000000}}

So it replaces the last digits with 0s.
Also if I try to log the response.body() I get:

ERRO[0001] TypeError: Value is not an object: {"item":{"id":2733382000000000049}}

The maximum integer that javascript can safely work with is much smaller:

> console.log(Number.MAX_SAFE_INTEGER)
9007199254740991

So any way to bypass this in k6 without changing the backend code?

2

Answers


  1. Use response.text() instead, then you MAY be able to do something

    // text would be the result of response.text()
    const text = '{"item":{"id":2733382000000000049}}'
    const fix = text.replace(/(d{15,})/g, '"$1"');
    console.log(JSON.parse(fix))

    Since I have no clue about the rest of the JSON you actually get, this may well be a very naive solution.

    Based on what you’ve shown, it would work.

    Login or Signup to reply.
  2. One solution is:

    Preprocessing your string from API to convert it into string before parsing.
    Optionally, you could convert it back into number for your own purpose.
    Here is the RegExp to convert all numbers in your string (proceeded with 🙂 into strings:

     // convert all number fields into strings to maintain precision
     // : 922271061845347495, => : "922271061845347495",
     stringFromApi = stringFromApi.replace(/:s*(-?d+),/g, ': "$1",');
    

    Regex explanation:

    s* any number of spaces
    -? one or zero ‘-‘ symbols (negative number support)
    d+ one or more digits
    (…) will be put in the $1 variable

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search