mercredi 17 novembre 2021

Pythonic way to check nested keys in dict

I'm listening a webhook that have the following structure:

{
"date": "17-11-2021"
"chatStarted": {
   "info": "0219302139",
   "chat_id": "123"

},
"messages": {
   "receivedMessage": {
      "text": "hi",
      "chat_id": "123"
},
   "sentMessage": {
      "text": "hello",
      "chat_id": "123"
}
},
"chatEnded": {
   "info": "0219302139",
   "chat_id": "123"
}
}

The keys chatStarted, receivedMessage, sentMessage and chatEnded are all optional, except that should be at least one of them, and chatStarted and chatEnded can't come together.

Some examples

1) When a chat starts:

{
"date": "17-11-2021"
"chatStarted": {
   "info": "0219302139",
   "chat_id": "123"


},
"messages": {
   "receivedMessage": {
      "text": "hi",
      "chat_id": "123"
}
}
}

2) When a chat ends:

{
"date": "17-11-2021"
"chatEnded": {
   "info": "0219302139",
   "chat_id": "123"

},
"messages": {
   "sentMessage": {
      "text": "bye",
      "chat_id": "123"
}
}
}

3) Random conversation

{
"date": "17-11-2021"
"messages": {
   "receivedMessage": {
      "text": "hi",
      "chat_id": "123"
},
   "sentMessage": {
      "text": "hello",
      "chat_id": "123"
}
}
}

What i want to do:

I would like to check if those keys exists to put the key on file name with chat_id.

Example:

if a received chatStarted and receivedMessage my file name will be:

123.chatStarted.receivedMessage.json

123 = chat_id (inside all keys).

I would like to know the pythonic way to do so, i'm currently using:


file_name = ''

if 'chatStarted' in r:
    
     x = r['chatStarted']['chat_id']
     file_name.join('chatStarted', '.')

if 'chatEnded' in r:
    
     x = r['chatEnded']['chat_id']
     file_name.join('chatEnded', '.')
    

if 'messages' in r:
    
    try: 
        x = r['messages']['receivedMessage']['chat_id']
        file_name.join('receivedMessage', '.')
    except:
        print('No receivedMessage')

    try: 
        x = r['messages']['sentMessage']['chat_id']
        file_name.join('sentMessage', '.')
    except:
        print('No sentMessage')

Aucun commentaire:

Enregistrer un commentaire