MDN self-hosted library · e01.org

RTCSessionDescription: toJSON() method

WebRTC

WebRTC

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

toJSON()

Parameters

None.

Return value

A JSON-serializable plain object containing the following properties:

  • type
  • sdp

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.

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.

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

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

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

Specifications

Browser compatibility

See also