PaymentAddress: toJSON() method

Deprecated

Avoid using this feature in new projects. This feature may be a candidate for removal from web standards or browsers.

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

Non-standard: This feature is not standardized. We do not recommend using non-standard features in production, as they have limited browser support, and may change or be removed. However, they can be a suitable alternative in specific cases where no standard option exists.

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

The method is automatically called by JSON.stringify() when a PaymentAddress 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

Given a PaymentAddress object named address, calling toJSON() directly returns a plain object. Its properties have the same values as the corresponding properties of the original object.

js
const json = address.toJSON();
console.log(json); // A plain object
console.log(typeof json); // "object"
console.log(json.country); // Same value as address.country

Serializing to a JSON string

The PaymentAddress object is serialized using JSON.stringify(), which calls the toJSON() method automatically.

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

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

json
{
  "country": "US",
  "addressLine": ["123 Main Street"],
  "region": "CA",
  "city": "San Francisco",
  "dependentLocality": "",
  "postalCode": "94105",
  "sortingCode": "",
  "organization": "",
  "recipient": "Alex Smith",
  "phone": ""
}

Browser compatibility