json - add a variable conditionally in python code -


i new python , wondering in below python code how add variable conditionally.

requestbody = json.dumps({"accountid":accountid,         "emailsubject":customdata.emailsubject,         "emailblurb":customdata.emailblurb,         "customfields":customfields,         "status":customdata.status,         "messagelock":customdata.messagelock}) 

like example want "custom"fields:customfields included if not null else not.how this??

create dictionary , add keys it, dump it. general way of doing things allows use different logic each key (e.g. if given key > 10).

to_json = {"accountid":accountid,     "emailsubject":customdata.emailsubject,     "emailblurb":customdata.emailblurb,     "customfields":customfields,     "status":customdata.status,     "messagelock":customdata.messagelock}  if james not none:   to_json['james'] = james  requestbody = json.dumps(to_json) 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -