Receiving a k6 response that includes a JSON object with a lengthy integer value

During a performance test, I encountered an issue where the response contained items like:

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

When parsed using k6's response.json(), it appeared as :

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

This resulted in the last digits being replaced with 0s. Additionally, attempting to log the response.body() produced an error:

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

It seems that JavaScript has limitations in handling large integers, as shown by the maximum safe integer value:

> console.log(Number.MAX_SAFE_INTEGER)
9007199254740991

Is there any way to work around this limitation in k6 without making changes to the backend code?

Answer №1

Try using response.text() in place of your current code, as it may allow you to perform the desired action

// The 'text' variable will contain the response from response.text()
const text = '{"item":{"id":2733382000000000049}}'
const fix = text.replace(/(\d{15,})/g, '"$1"');
console.log(JSON.parse(fix))

It's important to note that without knowing the full JSON structure you are working with, this solution may be simplistic.

However, based on the information provided, it should function correctly.

Answer №2

Here is a possible solution:

To ensure accuracy, preprocess the string received from the API by converting it to a string before parsing. Alternatively, you can revert it back to a number based on your requirements. Below is a regular expression (RegExp) that converts all numbers in the string (preceded with :) into strings:

 // Ensure precision by converting number fields into strings
 // : 922271061845347495, => : "922271061845347495",
 stringFromApi = stringFromApi.replace(/:\s*(-?\d+),/g, ': "$1",');

Explanation of the RegExp:

\s* represents any number of spaces
-? indicates one or zero occurrences of '-' (for negative numbers)
\d+ signifies one or more digits
(...) denotes the captured value stored in the $1 variable

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery's z-index feature is malfunctioning

When I hover over my menu, a box fades in. However, there is a small icon behind this box that I want to move to the front so it can be visible during hover. To see an example of my menu, click here: Navigation I attempted to address this by using jQuer ...

Encountering difficulty extracting information from an XML document within the Parse Cloud utilizing the XMLReader in an Express application

My goal is to extract elements from an XML file that I have already stored on the Parse Cloud for my Express app. I began coding after finding a helpful resource on using XMLReader with XPath on cloud code, as well as a guide on Parse httpRequest for retri ...

The JSON list is not being properly flattened

I have a list displayed in a column format within a data frame. The variables change across the items, and the column affiliations may not always be present. I've been attempting to transform the list into either a flattened data frame or a list con ...

AngularJS: How can components effectively communicate with each other using best practices?

I've been struggling to figure out how to facilitate communication between components. The primary question that has stumped me is: when should I opt for $watch versus $on ($broadcast/$emit) to establish component communication? I've identified ...

The cascading menu causes interference with the function of other elements in the

I am currently designing a navigation bar that includes a drop-down menu (with plans to add another one in the future). The issue I'm facing is that when the user clicks on the drop-down menu, it shifts all of the navigation links to the right. What I ...

Utilizing the click event with a Bootstrap modal: A guide

I have a PHP script that generates multiple Bootstrap modals, and I want to be able to modify some input fields when the "save changes" button is clicked. The ModalIDs generated are in the format "ModalID0000". However, nothing happens when I click on the ...

What are the benefits of using a combination of design patterns in JavaScript?

Currently, I am working on a personal project for learning purposes, which is a simple To-Do List. I am implementing the modular pattern (specifically, the revealing module pattern). The image below showcases my general idea of how I intend to build it. V ...

Best practices for handling null variable assignment in DataWeave

Converting true/false JSON input to Y/N char output is the goal of this transformation. varOutput: ('Y' when payload.varInput otherwise 'N') However, what happens if varInput is null? An exception might occur. To handle this, we can a ...

Looped values may not be compatible with Sendgrid substitutions, however, hardcoding the values should function properly

Currently, I am experimenting with Sendgrid's transactional templates at my workplace. Initially, I could successfully send mail with hardcoded values, but encountered issues when attempting to use variables. Let's take a closer look: My headers ...

Customizing listview appearance in asp.net with css and enhancing with javascript

When I print, the css class is not being used although it appears when using the program <script type="text/javascript"> function printDiv() { var divToPrint = document.getElementById('DivIdToPrint'); ...

Using Javascript to delete a cookie that was created by an AJAX response

Within my Webapp (which is currently running at localhost/myApp/), I am utilizing an ajax call as shown below: $.ajax({ type: "GET", url: "http://localhost:8080/my.module/Login/login?user=abc&password=123", xhrFields: { withCredent ...

Webpack configuration for asynchronous loading

I'm having trouble making this work. Can someone please assist? :) (According to the documentation, webpack is capable of handling Promises) Here is what works for me: var compiler = webpack(webpackConfig) However, when I try using a promise, I en ...

AngularJS Update Parent-Child Relationship: Enhancing the Details

When Panel header is clicked, I want to update the respective Panel body with the corresponding data. In my Angular code on Plunker, clicking "Test1" should only update "Test1detail" in the body. Click here for the Plunker Here's the code snippet fr ...

Introducing additional choices to the list and automatically refreshing the list with the latest updates

I am currently honing my skills in Yii2 by working on a project using this framework. One of the challenges I am facing is adding new list options dynamically without having to navigate away from the current page. When I click the "Add new option" button ...

Extract a specific value from a JSON object based on a matching key with a given search term using SQL

Within my Google BigQuery database, I encounter a column in a table that stores JSON objects with various attributes. select JSON '[{"id":"A","value":"1"},{"id":"B","value":"2"},{"id":"C","value":"John"},{"id":"D","value":null},{"id":"E","value":"rand ...

Utilizing localstorage data in angular 2: A comprehensive guide

Is there a way to utilize data stored in localstorage for another component? This is what the localstorage service looks like: localStorage.setItem('currentUser', JSON.stringify({ username: username, token: success, res: res.data })); I am inte ...

Accessing the JSON file from the Google Maps Places API using either JavaScript or PHP

In the midst of developing an application, I am working on fetching a list of places near a specific latitude and longitude. After obtaining an API key, inputting it into the browser URL successfully retrieves a JSON file containing the desired places dat ...

Error: missing semicolon prior to the statement

I am looking to create a Java web application that includes a JSP page. However, when I load the page, I encounter the following error: SyntaxError: missing ; before statement The code for my JSP page is as follows: <%@ page language="java" contentTy ...

What is the best way to utilize moment.js for adding days while excluding weekends?

I have a requirement to set a default follow-up date that is two days ahead of the current date. The existing code for this functionality is as follows: const Notify = moment().add(2, 'days').toDate(); Now, I need to modify this code to exclude ...

What is the best way to add table pagination at the bottom of a table?

Can someone provide guidance on implementing table pagination for the material-ui table below? The documentation is a bit unclear: <Table ria-label="a dense table"> <TableHead> <TableRow> ...