The essence of the question is how to derive a random key/value from a JSON dictionary.
def randkey():
with open("file.json") as file:
dict = json.load(file)
for k, v in sorted(dict.items())[-1:]:
randkeyvalue = f"Name: {v['Name']}\n" \
f"Age: {v['Age']}\n"
If I can sort the dictionary in reverse order (sorted()
) and take the very first value from it [-1:]
. Can I sort it randomly and take the first value and of course it will always be different?