0

I have no experience with python, just started looking into this week:

messages = []
msg_list = ticket.message

for message in msg_list:
for item in msg_list:
    item_json = json.loads(message.body)

    tmp_item.date = item_json['date']
    tmp_item.time = item_json['time']
    tmp_item.author = item_json['author']
    tmp_item.location = item_json['location']
    tmp_item.message = item_json['message']

    msg_list.append(tmp_item)

return {"payload": msg_list}

Is there a way I can check if the item_json(message.body) does not have the following props, "date", "time", "author", "location" and "message" to simply avoid it,and do not append to msg_list??

So basically I just want to append if it meets that criteria, example would be

if item_json['date'] and item_json['time'] and item_json['author'].... :
bernlt
  • 405
  • 2
  • 8
  • 20

1 Answers1

1

What you refer to as json data (after parsing) is actually a dict in python. To check whether a key exists in a dictionary the most common way is to use in operator

if 'key' in dictionary:
    print(dictionary['key']) # if key exists
else:
    print("Key doesn't exist")

See Check if a given key already exists in a dictionary

Galunid
  • 578
  • 6
  • 14