NavigatorUAData: 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.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Note: This feature is available in Web Workers.
The toJSON() method of the NavigatorUAData interface returns a JSON-serializable plain object representing the NavigatorUAData object. Only the low-entropy values are included. To get high-entropy values, use NavigatorUAData.getHighEntropyValues().
The method is automatically called by JSON.stringify() when a NavigatorUAData object is encountered during JSON serialization.
Syntax
toJSON()
Parameters
None.
Return value
A JSON-serializable plain object containing the following properties:
brands: the array and its objects withbrandandversionproperties are copied into a new array and new objectsmobileplatform
Unless stated otherwise, each property's value is copied as-is.
Examples
>Calling toJSON() directly
Calling toJSON() directly on navigator.userAgentData returns a plain object. Its properties have the same values as the corresponding properties of the original object.
const json = navigator.userAgentData.toJSON();
console.log(json); // A plain object
console.log(typeof json); // "object"
console.log(json.mobile); // Same value as navigator.userAgentData.mobile
Serializing to a JSON string
The NavigatorUAData object is serialized using JSON.stringify(), which calls the toJSON() method automatically.
console.log(JSON.stringify(navigator.userAgentData));
This would log a JSON string like so (formatted for readability):
{
"brands": [
{
"brand": "Chromium",
"version": "126"
},
{
"brand": "Not/A)Brand",
"version": "8"
}
],
"mobile": false,
"platform": "Windows"
}
Specifications
| Specification |
|---|
| User-Agent Client Hints> # dom-navigatoruadata-tojson> |