Home > Back-end >  Python - parking JSON file and stucked at empty .items()
Python - parking JSON file and stucked at empty .items()

Time:03-05

So I am parsing some JSON file and it always breaks when code gets to one that has empty '' value for .items():

for i,n in v['objects'].items():

I get:

AttributeError
'str' object has no attribute 'items'

Whenever code gets to some item that has field:

"objects": ""

Does anyone know how to handle this? I tried with checking first if it is empty, but no success. I tried with this before my FOR:

objects_empty = ""
if not v['objects'].items() == objects_empty:

Thanks!

CodePudding user response:

[From the earlier comment]

v['objects'] is the value that can be an empty string instead of a dictionary. Hence, you want to check whether this value equals the empty string and not its .items(). A string doesn't have items, that's what the error message complains about.

You can change it to this:

if v["objects"] != "":
    ...
  • Related