PressureRecord: toJSON() method

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The toJSON() method is a serializer; it returns a JSON representation of the PressureRecord object.

Syntax

js
toJSON()

Parameters

None.

Return value

A JSON object that is the serialization of the PressureRecord object.

Examples

Using the toJSON method

In this example, calling lastRecord.toJSON() returns a JSON representation of the PressureRecord object.

js
function callback(records) {
  const lastRecord = records[records.length - 1];
  console.log(lastRecord.toJSON);
}

try {
  const observer = new PressureObserver(callback);
  await observer.observe("cpu", {
    sampleInterval: 1000, // 1000ms
  });
} catch (error) {
  // report error setting up the observer
}

This would log a JSON object like so:

json
{
  "source": "cpu",
  "state": "fair",
  "time": 1712052746385.347
}

To get a JSON string, you can use JSON.stringify(lastRecord) directly; it will call toJSON() automatically.

Specifications

Specification
Compute Pressure Level 1
# the-tojson-member

Browser compatibility

BCD tables only load in the browser

See also