Skip to content Skip to sidebar Skip to footer

Checking If JSON Key Is Empty

I'd like to be able to check if a JSON key is empty, then have the script exit depending on the result. I have the following JSON: { 'changed': false, 'results': [] } If t

Solution 1:

Check both, the key existence and its length:

import json, sys

obj=json.load(sys.stdin)

if not 'results' in obj or len(obj['results']) == 0:
    exit(0)
else:
    exit(1)

Solution 2:


Post a Comment for "Checking If JSON Key Is Empty"