简体   繁体   中英

Filter JSON using IP Address Range (JavaScript)

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. (ie 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)"
}]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM