Javascript – How to iterate over data in JSON file using D3 javascript

d3.jsjavascriptjqueryjson

I am trying to iterate over and retrieve some data from JSON file using D3 Javascript.
Here is the JSON file:

{
    "Resources": 
    [
    {
        "subject": "Node 1",
        "group" : "1"
    }, 
    {
        "predicate": "Node 2",
        "group" : "2"
    }, 
    {
        "object": "Node 3",
        "group" : "3"
    }, 
    {
        "subject": "Node 4",
        "group" : "4"
    }, 
    {
        "predicate": "Node 5",
        "group" : "5"
    }, 
    {
        "object": "Node 6",
        "group" : "6"
    }
    ]
}

This is my code in D3 Javascript for iterating and retrieving data:

d3.json("folder/sample.json", function(error, graph) {

  document.write(graph.Resources[0].subject);
  // The code for retrieving all the elements from the JSON file
});

The code above retrieves the first subject which is: Node 1. I could not even retrieve the group.
Could anyone please help me iterate over the JSON file Resources and retrieve the elements: subject, predicate, object and group, using any sort of iterations such as a for loop.

Best Solution

The group lines in your JSON file should look like "group" : "2". Also, your JSON contains a single object (Resources); that's why your document.write is only called once. You'll need to iterate through the value of Resources:

d3.json("test.json", function(error, graph) {
    var resources = graph.Resources;
    for (var i = 0; i < resources.length; i++) {
        var obj = resources[i]
        for (var key in obj) {
            console.log(key+"="+obj[key]);
        }   
    }   
});

will get you

subject=Node 1
group=1
...