1

I have a JSON file where every JSON object has a unique identifier (IP Address)

When given a range of IP Addresses, I want to filter the Original JSON based on the given range and store it in a new file. (i.e All the JSON objects having an IP Address that falls in the given Range)

JSON File:

[{
    "Name": "SERVER1",
    "ipv4Address": "192.168.0.50",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
},
{
    "Name": "SERVER2",
    "ipv4Address": "192.168.0.51",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
},
{
    "Name": "SERVER3",
    "ipv4Address": "192.168.0.52",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
},
{
    "Name": "SERVER4",
    "ipv4Address": "192.168.0.53",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
},
{
    "Name": "SERVER5",
    "ipv4Address": "192.168.0.54",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
}]

If the given range were 192.168.0.52 - 192.168.0.54, the output should be:

 [{
    "Name": "SERVER3",
    "ipv4Address": "192.168.0.52",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
},
{
    "Name": "SERVER4",
    "ipv4Address": "192.168.0.53",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
},
{
    "Name": "SERVER5",
    "ipv4Address": "192.168.0.54",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
}]
Vinil Vasani
  • 123
  • 3
  • 9
  • For starters learning how to filter arrays is an easy web search. Please show what you have tried. Then you haven't mentioned where you will run this code ... in browser or on server. You can't write to file using client side code – charlietfl Oct 20 '18 at 16:02

1 Answers1

1

use below code snippet.

function isWithinRange(ip, lowerBound, upperBound) {

  // Put all IPs into one array for iterating and split all into their own 
  // array of segments
  var ips = [ip.split('.'), lowerBound.split('.'), upperBound.split('.')];

  // Convert all IPs to ints
  for(var i = 0; i < ips.length; i++) {

    // Typecast all segments of all ips to ints
    for(var j = 0; j < ips[i].length; j++) {
      ips[i][j] = parseInt(ips[i][j]);
    }

    // Bit shift each segment to make it easier to compare
    ips[i] = 
      (ips[i][0] << 24) + 
      (ips[i][1] << 16) + 
      (ips[i][2] << 8) + 
      (ips[i][3]);
  }

  // Do comparisons
  if(ips[0] >= ips[1] && ips[0] <= ips[2]) return true;

  return false;
}


var json = [{
    "Name": "SERVER1",
    "ipv4Address": "192.168.0.50",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
},
{
    "Name": "SERVER2",
    "ipv4Address": "192.168.0.51",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
},
{
    "Name": "SERVER3",
    "ipv4Address": "192.168.0.52",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
},
{
    "Name": "SERVER4",
    "ipv4Address": "192.168.0.53",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
},
{
    "Name": "SERVER5",
    "ipv4Address": "192.168.0.54",
    "OperatingSystem": [],
    "OperatingSystemServicePack": null,
    "OperatingSystemVersion": "6.3 (9600)"
}];


var newArr = [];
var lowerBound = '192.168.0.52';
var upperBound = '192.168.0.54';

json.forEach(function(item){
      var isInRange = isWithinRange(item.ipv4Address, lowerBound, upperBound);
      if(isInRange){ 
        newArr.push(item);
      }
});


console.log("new Array : ", newArr);

im using solution provided by @SaltedBlowfish using his Bit Shifting formula

ref: https://stackoverflow.com/a/29173281/4882013

Shiv Kumar Baghel
  • 2,464
  • 6
  • 18
  • 34