1

How can if check is some fields exist in a json object ?

  v  = {         
      "f1":"v1",
      "f2":"v2",
      "f3":"v3",
      "f4":"v4",
      "f5":"v5"
    }

I have this code:

if v['f1']:
  print "f1 exists"
elif v['f2']:
  print "f2 exists"
elif v['f3']:
  print "f3 exists"

But sometime, when a field doen't existe, exception is fired and the other test fail. How can I do this test ?

dmx
  • 1,862
  • 3
  • 26
  • 48
  • 1
    This question is not an exact duplicate of the question given. This question is about json objects which python sees as dicts, but the answer needs to be given that json objects are the same as python dicts and are handled as such. Now go look at http://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary – whitebeard Jul 21 '16 at 10:23

0 Answers0