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)

ifnot'results'in obj orlen(obj['results']) == 0:
    exit(0)
else:
    exit(1)

Solution 2:

import json, sys

obj=json.load(sys.stdin)

iflen(obj["results"])==0:
    exit(0)
else:
    exit(1)

try using the length of obj["results"]

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