Skip to content

Instantly share code, notes, and snippets.

@mzubairsaleem
Last active September 4, 2020 22:15
Show Gist options
  • Save mzubairsaleem/c06b4ca5678939731dbd1a872daa17f7 to your computer and use it in GitHub Desktop.
Save mzubairsaleem/c06b4ca5678939731dbd1a872daa17f7 to your computer and use it in GitHub Desktop.
Nested JSON Object Parser in Ruby
{
"http_method":"GET",
"revision":"1598475252.192415",
"results":{
"port1":{
"id":"port1",
"name":"port1",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":-1,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port2":{
"id":"port2",
"name":"port2",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port3":{
"id":"port3",
"name":"port3",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port4":{
"id":"port4",
"name":"port4",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port5":{
"id":"port5",
"name":"port5",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port6":{
"id":"port6",
"name":"port6",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port7":{
"id":"port7",
"name":"port7",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port8":{
"id":"port8",
"name":"port8",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port9":{
"id":"port9",
"name":"port9",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port10":{
"id":"port10",
"name":"port10",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port11":{
"id":"port11",
"name":"port11",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port12":{
"id":"port12",
"name":"port12",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port13":{
"id":"port13",
"name":"port13",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port14":{
"id":"port14",
"name":"port14",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port15":{
"id":"port15",
"name":"port15",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port16":{
"id":"port16",
"name":"port16",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port17":{
"id":"port17",
"name":"port17",
"alias":"(PDHQ)",
"mac":"00:00:00:00:00:00",
"ip":"10.14.90.2",
"mask":16,
"link":true,
"speed":1000,
"duplex":1,
"tx_packets":164232531,
"rx_packets":202055095,
"tx_bytes":123827505113,
"rx_bytes":153231262466,
"tx_errors":0,
"rx_errors":0
},
"port18":{
"id":"port18",
"name":"port18",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port19":{
"id":"port19",
"name":"port19",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port20":{
"id":"port20",
"name":"port20",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port21":{
"id":"port21",
"name":"port21",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port22":{
"id":"port22",
"name":"port22",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port23":{
"id":"port23",
"name":"port23",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port24":{
"id":"port24",
"name":"port24",
"alias":"(HA)",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":true,
"speed":1000,
"duplex":1,
"tx_packets":675294278,
"rx_packets":535960811,
"tx_bytes":368209044656,
"rx_bytes":141276391446,
"tx_errors":0,
"rx_errors":0
},
"port25":{
"id":"port25",
"name":"port25",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port26":{
"id":"port26",
"name":"port26",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port27":{
"id":"port27",
"name":"port27",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port28":{
"id":"port28",
"name":"port28",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port29":{
"id":"port29",
"name":"port29",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port30":{
"id":"port30",
"name":"port30",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port31":{
"id":"port31",
"name":"port31",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port32":{
"id":"port32",
"name":"port32",
"alias":"(HA)",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":true,
"speed":1000,
"duplex":1,
"tx_packets":56472557,
"rx_packets":56479224,
"tx_bytes":32528192376,
"rx_bytes":32419072572,
"tx_errors":0,
"rx_errors":0
},
"port33":{
"id":"port33",
"name":"port33",
"alias":"(to Cores Port 1\/36)",
"mac":"00:00:00:00:00:00",
"ip":"10.1.80.18",
"mask":28,
"link":true,
"speed":10000,
"duplex":1,
"tx_packets":49392779848,
"rx_packets":31394748957,
"tx_bytes":42612188881340,
"rx_bytes":13307241792299,
"tx_errors":0,
"rx_errors":0
},
"port34":{
"id":"port34",
"name":"port34",
"alias":"(to Cores Port 2\/1)",
"mac":"00:00:00:00:00:00",
"ip":"10.1.80.34",
"mask":28,
"link":true,
"speed":10000,
"duplex":1,
"tx_packets":998054,
"rx_packets":6944152,
"tx_bytes":81840258,
"rx_bytes":490075693,
"tx_errors":0,
"rx_errors":0
},
"port35":{
"id":"port35",
"name":"port35",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port36":{
"id":"port36",
"name":"port36",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port37":{
"id":"port37",
"name":"port37",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port38":{
"id":"port38",
"name":"port38",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port39":{
"id":"port39",
"name":"port39",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"port40":{
"id":"port40",
"name":"port40",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"204.76.144.27",
"mask":28,
"link":true,
"speed":10000,
"duplex":1,
"tx_packets":37210456124,
"rx_packets":50847544033,
"tx_bytes":14829106692588,
"rx_bytes":42477767627222,
"tx_errors":0,
"rx_errors":0
},
"mgmt1":{
"id":"mgmt1",
"name":"mgmt1",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"192.168.1.99",
"mask":24,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"mgmt2":{
"id":"mgmt2",
"name":"mgmt2",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"modem":{
"id":"modem",
"name":"modem",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":false,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"npu0_vlink0":{
"id":"npu0_vlink0",
"name":"npu0_vlink0",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":true,
"speed":0,
"duplex":0,
"tx_packets":6901,
"rx_packets":9474,
"tx_bytes":5440424,
"rx_bytes":342863,
"tx_errors":0,
"rx_errors":0
},
"npu0_vlink1":{
"id":"npu0_vlink1",
"name":"npu0_vlink1",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":true,
"speed":0,
"duplex":0,
"tx_packets":9474,
"rx_packets":6901,
"tx_bytes":342863,
"rx_bytes":5440424,
"tx_errors":0,
"rx_errors":0
},
"npu1_vlink0":{
"id":"npu1_vlink0",
"name":"npu1_vlink0",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":true,
"speed":0,
"duplex":0,
"tx_packets":5618,
"rx_packets":0,
"tx_bytes":0,
"rx_bytes":3478051,
"tx_errors":0,
"rx_errors":0
},
"npu1_vlink1":{
"id":"npu1_vlink1",
"name":"npu1_vlink1",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"0.0.0.0",
"mask":0,
"link":true,
"speed":0,
"duplex":0,
"tx_packets":0,
"rx_packets":5618,
"tx_bytes":3478051,
"rx_bytes":0,
"tx_errors":0,
"rx_errors":0
},
"WWW_1WEST_NAT":{
"id":"WWW_1WEST_NAT",
"name":"WWW_1WEST_NAT",
"alias":"",
"mac":"00:00:00:00:00:00",
"ip":"204.76.149.2",
"mask":24,
"link":true,
"speed":10000,
"duplex":1,
"tx_packets":37210436588,
"rx_packets":50846387501,
"tx_bytes":14661792924686,
"rx_bytes":42252661852907,
"tx_errors":0,
"rx_errors":0,
"vlanid":804,
"interface":"port40"
}
},
"vdom":"root",
"path":"system",
"name":"interface",
"status":"success",
"serial":"FG1K5D3I14801563",
"version":"v6.2.3",
"build":1066
}
require "json"
file = open("data.json")
json = file.read
parsed = JSON.parse(json)
val = []
parsed["results"].each do |k, v|
p k
p v
val << v
end
p val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment