RTCSessionDescription: toJSON() method

Baseline
Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.

The toJSON() method of the RTCSessionDescription interface returns a JSON-serializable plain object representing the RTCSessionDescription object.

The method is automatically called by JSON.stringify() when an RTCSessionDescription object is encountered during JSON serialization.

Syntax

js
toJSON()

Parameters

None.

Return value

A JSON-serializable plain object containing the following properties:

Each property's value is copied as-is.

Examples

Calling toJSON() directly

This example creates an RTCSessionDescription object. Calling toJSON() directly returns a plain object. Its properties have the same values as the corresponding properties of the original object.

js
const description = new RTCSessionDescription({ type: "rollback", sdp: "" });

const json = description.toJSON();
console.log(json); // A plain object
console.log(typeof json); // "object"
console.log(json.type); // Same value as description.type

Serializing to a JSON string

The RTCSessionDescription object from the previous example is serialized using JSON.stringify(), which calls the toJSON() method automatically.

js
console.log(JSON.stringify(description));

This would log a JSON string like so (formatted for readability):

json
{
  "type": "rollback",
  "sdp": ""
}

Specifications

Specification
WebRTC: Real-Time Communication in Browsers
# dom-rtcsessiondescription-tojson

Browser compatibility

See also