Home > Software engineering >  How do I access json object elements from this json in JS?
How do I access json object elements from this json in JS?

Time:04-01

I have this Json object and need to consume latitude and longitude

Object {
 "coords": Object {
   "accuracy": 22.512498028738957,
   "altitude": 56.650177001953125,
   "altitudeAccuracy": 9.316750526428223,
   "heading": -1,
   "latitude": 44.63884296672757,
   "longitude": -63.59112091788229,
   "speed": -1,
  },
  "timestamp": 1648821089319.792,
}

CodePudding user response:

It's not a JSON file, it's an Object.

Just save the Object in a variable like obj and access it through

obj.coords.latitude , or obj[coords][latitude].

CodePudding user response:

it is an object, not json. so you can

obj.coords.latitude

An example:

let obj = {
  coords: {
   accuracy: 22.512498028738957,
   altitude: 56.650177001953125,
   altitudeAccuracy: 9.316750526428223,
   heading: -1,
   latitude: 44.63884296672757,
   longitude: -63.59112091788229,
   speed: -1,
  },
  timestamp: 1648821089319.792,
}


console.log(obj.coords.latitude)

  • Related