Sush
Sush

Reputation: 1457

Filter JSON data based on a value

I have the following JSON data:

{
  "VMs":[
    {
      "ID":"VM-WIN7-64",
      "OS":"Windows 7",
      "FLAVOUR":"VM-IE8-001-preq",
      "ADAPTER":"Win 9",
      "Paths":"D:\\VirtualMachines\\Win7_X64_VM-001\\Win7_X64_VM-001.vmx"

    },
{
      "ID":"VM-WIN7-6",
      "OS":"Windows jj7",
      "FLAVOUR":"VM-IE8-001-preq",
      "ADAPTER":"Winjjjjj 9",
      "Paths":"f:\\VirtualMachines\\Win7_X64_VM-001\\Win7_X64_VM-001.vmx"

    }
  ]

}

In this JSON, I am getting the value "ID":"VM-WIN7-64". Using this ID, how can I filter the corresponding adapter name "ADAPTER":"Win 9" from this JSON data?

Upvotes: 0

Views: 610

Answers (1)

tewathia
tewathia

Reputation: 7298

Use the Array.prototype.filter method.

var filtered = data.VMs.filter(function (element) {
    return element.ID == "VM-WIN7-64";
});

(where the data variable contains your entire JSON data "VMs" array)

Then, filtered[0].ADAPTER would have the value "Win 9"

DEMO

Upvotes: 1

Related Questions