Parse JSON file downloaded from API and modify for D3.js graphs

Viewed 53

I have been working on D3.js to get my network topology graph. as of now manually I can put data inside an json file and it works:-

main.json :- ########################################################

{
  "nodes": [
    { "name": "DEVICE-A" },
    { "name": "DEVICE-B" },
    { "name": "DEVICE-C" },
    { "name": "NET-A" },
    { "name": "NET-B" },
    { "name": "NET-C" }
  ],

  "links": [    
    { "source": "DEVICE-A", "target": "NET-A" },
    { "source": "DEVICE-A", "target": "NET-B" },
    { "source": "DEVICE-B", "target": "NET-A" },
    { "source": "DEVICE-B", "target": "NET-B" },
    { "source": "DEVICE-C", "target": "NET-A" },
    { "source": "DEVICE-C", "target": "NET-B" },
    { "source": "DEVICE-C", "target": "NET-C" }
  ]
}

##########################################################

now my need is create main.json file automatically using the information fetched via API. below received request from API call contains all the information for Devices and Network attached.

####################################

{
  "listvm": {
    "count": 3,
    "virtualmachine": [
      {
        "name": "DEVICE-A",
        "state": "Running",
        "nic": [
          {
            "networkname": "NET-A",
            "ipaddress": "172.16.200.14"
          },
          {
            "networkname": "NET-B",
            "ipaddress": "172.16.120.14"
          }
        ],
        "tags": []
      },
      {
        "name": "DEVICE-B",
        "state": "Running",
        "nic": [
          {
            "networkname": "NET-A",
            "ipaddress": "172.16.200.15"
          },
          {
            "networkname": "NET-B",
            "ipaddress": "172.16.120.15"
          }
        ],
        "tags": []
      },
      {
        "name": "DEVICE-C",
        "state": "Running",
        "nic": [
          {
            "networkname": "NET-A",
            "ipaddress": "172.16.200.16"
          },
          {
            "networkname": "NET-B",
            "ipaddress": "172.16.120.16"
          },
          {
            "networkname": "NET-C",
            "ipaddress": "192.168.210.16"
          }
        ],
        "tags": []
      }
    ]
  }
}

####################################################

can someone suggest how to make use of API result and generate main.json.

0 Answers
Related