0

I wan't to access to a specific level of a JSON who look like this :

var data =  { "Nodes": [{"lvl": 0, "name": "830010000C2733", "date_modification": ""},{"lvl":1, "name": "830010000C2219", "date_modification": "2014-04-28"}],"links": [{"source": "830010000C2733", "target": "830010000C2219"}] };

I can access to the full data with console.log(data), but I can't access to my Nodes level. I tried console.log(data.Nodes) but the browser returns me data.Nodes is undefined.

How to access to this level and in general to a specific level of a JSON?

GeoGyro
  • 487
  • 12
  • 32
  • 1
    That's not Json, it's a Javascript object with an array and more objects inside. data.Nodes[0].name for example will give you "830010000C2733" – alotropico Aug 25 '20 at 08:09
  • 2
    The browser will never print this output when you do `console.log(data.Nodes)`. It will either just print `undefined` or it will throw an error that `data` is not defined. Please provide more details of what you are doing. My initial guess is that you are trying to access some data before it was asynchronously loaded (i.e. it has nothing to do with the question you actually asked, which would be a duplicate of [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/q/11922383/218196) anyway. – Felix Kling Aug 25 '20 at 08:13
  • I'm building a web page with a `Leaflet` map and a `D3` chart. When user over an object in the map, a function get the `JSON` and use it for to build the graph. My chart function is before the main map function. I thought about asynchronous issues but the `JSON` seems to be well loaded in `var data`. – GeoGyro Aug 25 '20 at 10:19
  • You we're right @alotropico, I used `JSON.parse(data)` to fix this and everything ok. – GeoGyro Aug 25 '20 at 13:37

1 Answers1

0

Don't know what you did wrong but it works like you said.

var data =  { "Nodes": [{"lvl": 0, "name": "830010000C2733", "date_modification": ""},{"lvl":1, "name": "830010000C2219", "date_modification": "2014-04-28"}],"links": [{"source": "830010000C2733", "target": "830010000C2219"}] };

console.log(data.Nodes)
Wimanicesir
  • 4,606
  • 2
  • 11
  • 30