Home > Mobile >  Using JSON in python
Using JSON in python

Time:10-20

Hello i am wondering how i can get from json data like in matrix? for better explenation:

{
     "00": "Echo reply",
     "03":{ "00" : "Destination Unreachable - Net Unreachable",
          "01" : "Destination Unreachable - Host Unreachable",
          "02" : "Destination Unreachable - Protocol Unreachable",
          "03" : "Destination Unreachable - Port Unreachable",
          "04" : "Destination Unreachable - Fragmentation Needed & DF Set",
          "05" : "Destination Unreachable - Source Route Failed",
          "06" : "Destination Unreachable - Destination Network Unknown",
          "07" : "Destination Unreachable - Destination Host unknown",
          "08" : "Destination Unreachable - Source Host Isolated",
          "09" : "Destination Unreachable - Network Administratively Prohibited",
          "0a" : "Destination Unreachable - Host Administratively Prohibited",
          "0b" : "Destination Unreachable - Network unreachable for TOS",
          "0c" : "Destination Unreachable - Host unreachable for TOS",
          "0d" : "Destination Unreachable - Communication Administratively Prohibited"
     },
     "04": "Source Quench",
     "05": {
          "00": "Redirect - Datagram for the Network",
          "01": "Redirect - Datagram for the Host",
          "02": "Redirect - Datagram for the TOS & Network",
          "03": "Redirect - Datagram for the TOS & Host"
     },
     "08": "Echo",
     "09": "Router Advertisement",
     "0a": "Router Selection",
     "0b": {
          "01":"Time Exceeded - Time to live exceeded in Transit",
          "02":"Time Exceeded - Fragment Reassembly Time Exceeded"
     },
     "0c": {
         "00": "Parameter Problem - Pointer indicates the error",
          "01": "Parameter Problem - Missing a Required Option",
          "02": "Parameter Problem - Bad Length"
     },
     "0d": "Timestamp",
     "0e": "Timestamp Reply",
     "0f": "Information Request",
     "10": "Information Reply",
     "11": "Address Mask Request",
     "12": "Address Mask Reply",
     "1e": "Traceroute"

}

This is my json File an i want to get from that "03" - > "00" but, idk how to get there

 f = open('icmp.json', )
 data = json.load(f)

 print(data[type, code])  #i was also trying data[type[code]] or data[type][code]

But not woriking for me ? :D please help

CodePudding user response:

when you load a json, it is a dictionary in python. you can get your item like this:

 f = open('icmp.json', )
 data = json.load(f)
 print(data['03']['00'])

CodePudding user response:

Changing my json file to this worked :) Thx all for your response

{
     "00": {"00": "Echo reply" },
     "03":{ "00" : "Destination Unreachable - Net Unreachable",
          "01" : "Destination Unreachable - Host Unreachable",
          "02" : "Destination Unreachable - Protocol Unreachable",
          "03" : "Destination Unreachable - Port Unreachable",
          "04" : "Destination Unreachable - Fragmentation Needed & DF Set",
          "05" : "Destination Unreachable - Source Route Failed",
          "06" : "Destination Unreachable - Destination Network Unknown",
          "07" : "Destination Unreachable - Destination Host unknown",
          "08" : "Destination Unreachable - Source Host Isolated",
          "09" : "Destination Unreachable - Network Administratively Prohibited",
          "0a" : "Destination Unreachable - Host Administratively Prohibited",
          "0b" : "Destination Unreachable - Network unreachable for TOS",
          "0c" : "Destination Unreachable - Host unreachable for TOS",
          "0d" : "Destination Unreachable - Communication Administratively Prohibited"
     },
     "04": {"00": "Source Quench"},
     "05": {
          "00": "Redirect - Datagram for the Network",
          "01": "Redirect - Datagram for the Host",
          "02": "Redirect - Datagram for the TOS & Network",
          "03": "Redirect - Datagram for the TOS & Host"
     },
     "08":{"00": "Echo " },
     "09":{"00": "Router Advertisement"},
     "0a": {"00": "Router Selection" },
     "0b": {
          "01":"Time Exceeded - Time to live exceeded in Transit",
          "02":"Time Exceeded - Fragment Reassembly Time Exceeded"
     },
     "0c": {
         "00": "Parameter Problem - Pointer indicates the error",
          "01": "Parameter Problem - Missing a Required Option",
          "02": "Parameter Problem - Bad Length"
     },
     "0d": {"00": "Timestamp" },
     "0e": {"00": "Timestamp Reply" },
     "0f": {"00": "Information Request"},
     "10": {"00": "Information Reply"},
     "11": {"00": "Address Mask Request"},
     "12": {"00": "Address Mask Reply"},
     "1e": {"00": "Traceroute"}
}
  • Related