ContactAddress: toJSON() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Want more browser support for this feature? Tell us why.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The toJSON() method of the ContactAddress interface returns a JSON-serializable plain object representing the ContactAddress object.
The method is automatically called by JSON.stringify() when a ContactAddress object is encountered during JSON serialization.
Syntax
toJSON()
Parameters
None.
Return value
A JSON-serializable plain object containing the following properties:
countryaddressLineregioncitydependentLocalitypostalCodesortingCodeorganizationrecipientphone
Each property's value is copied as-is.
Examples
>Calling toJSON() directly
Given a ContactAddress object named address, calling toJSON() directly returns a plain object. Its properties have the same values as the corresponding properties of the original object.
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 ContactAddress object is serialized using JSON.stringify(), which calls the toJSON() method automatically.
console.log(JSON.stringify(address));
This would log a JSON string like so (formatted for readability):
{
"country": "US",
"addressLine": ["123 Main Street"],
"region": "CA",
"city": "San Francisco",
"dependentLocality": "",
"postalCode": "94105",
"sortingCode": "",
"organization": "",
"recipient": "Alex Smith",
"phone": ""
}
Specifications
| Specification |
|---|
| Contact Picker API> # dom-contactaddress-tojson> |