-1

I have the following data from a response. This appears to be a dict.

I'm able to access, for example, "description". Can you please advise on what I should be reading to allow me to access the "tcp_options" bit. I'm assuming this is a nested dict.

  {
  "description": "sftp",
  "icmp_options": null,
  "is_stateless": false,
  "protocol": "6",
  "source": "127.0.0.1/32",
  "source_type": "CIDR_BLOCK",
  "tcp_options": {
    "destination_port_range": null,
    "source_port_range": {
      "max": 5500,
      "min": 5500
    }
  },
  "udp_options": null
  },
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • https://docs.python.org/3/tutorial/datastructures.html#dictionaries – jonrsharpe Jun 13 '20 at 18:28
  • You say that you can access `"description"`, so that means you can access `"tcp_options"`. Is there another problem you have not mentioned yet? – quamrana Jun 13 '20 at 18:28
  • All, especiallit quamrana. Yes I guess i should have mentioned, when trying to access the "tcp_options" I get. KeyError: 'tcp_options'. Sorry. – user1148158 Jun 13 '20 at 18:39

3 Answers3

0

To access an element in a nested dict in python, like this:

thing = { "hi":{"hello":"bye"}}

You would write:

print(thing["hi"]["hello"])

This should return the following:

>>> print(thing["hi"]["hello"])
bye
>>> ...

Hope this answers your question!

0

All,

Firstly thank you for your inputs. But it turns out I'm a bit of an idiot.

'tcp_options"':

Actually has an " in there and that's whay I was getting the error. I thought the " was some form of mystical python voodoo.

Sorry, but thank you all.

edit : my line of code: print(newRules['tcp_options"']['destination_port_range']['min']) now actually works.

0

Accessing the dict by its key value should do just fine:

dd = {
  "description": "sftp",
  "icmp_options": 'null',
  "is_stateless": 'false',
  "protocol": "6",
  "source": "127.0.0.1/32",
  "source_type": "CIDR_BLOCK",
  "tcp_options": {
    "destination_port_range": 'null',
    "source_port_range": {
      "max": 5500,
      "min": 5500
    }
  },
  "udp_options": 'null'
  }

for key,value in dd.items():
    print(key,value)


print(dd["tcp_options"])

OUTPUT:

description sftp                                                                                                                                                             
source_type CIDR_BLOCK                                                                                                                                                       
protocol 6                                                                                                                                                                   
source 127.0.0.1/32                                                                                                                                                          
icmp_options null                                                                                                                                                            
tcp_options {'source_port_range': {'max': 5500, 'min': 5500}, 'destination_port_range': 'null'}                                                                              
is_stateless false                                                                                                                                                           
udp_options null                                                                                                                                                             
{'source_port_range': {'max': 5500, 'min': 5500}, 'destination_port_range': 'null'}
DirtyBit
  • 16,613
  • 4
  • 34
  • 55