e01.org

PaymentAddress: toJSON() method

Payment Request API

Payment Request API

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](/docs/Glossary/JSON) serialization.

## Syntax

```js-nolint
toJSON()
```

### Parameters

None.

### Return value

A JSON-serializable plain object containing the following properties:

- country
- addressLine
- region
- city
- dependentLocality
- postalCode
- sortingCode
- organization
- recipient
- phone

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

source: MDN Web Docs mirror · page

Cite this

"PaymentAddress: toJSON() method". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/PaymentAddress/toJSON

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·