W3cubDocs

/Web APIs

GeolocationCoordinates: toJSON() method

Baseline 2024
Newly available

Since ⁨September 2024⁩, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

The toJSON() method of the GeolocationCoordinates interface is a serializer; it returns a JSON representation of the GeolocationCoordinates object.

Syntax

toJSON()

Parameters

None.

Return value

A JSON object that is the serialization of the GeolocationCoordinates object.

Examples

>

Using the toJSON() method

In this example, calling position.coords.toJSON() returns a JSON representation of the GeolocationCoordinates object.

navigator.geolocation.getCurrentPosition((position) => {
  console.log(position.coords.toJSON());
});

This would log a JSON object like so:

{
  "accuracy": 12.0,
  "latitude": 53.0,
  "longitude": 8.0,
  "altitude": null,
  "altitudeAccuracy": null,
  "heading": null,
  "speed": null
}

To get a JSON string, you can use JSON.stringify(position.coords) directly; it will call toJSON() automatically.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
toJSON 126 126 129 112 18 126 129 83 18 28.0 126 18

See also

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates/toJSON