PressureRecord: toJSON() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

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

Note: This feature is available in Web Workers, except for Service Workers.

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

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
toJSON
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.

See also