selecting random json element
<p><strong>python 3.5</strong></p>

<p>hi i have following json file and i want to select json data random...</p>

<p><strong>json</strong></p>

<pre><code>{"x":[
    {"A":"B"},
    {"A":"C"},
    {"F":"H"}
]}
</code></pre>

<p>select data where item in data['x'] is A </p>

<p>(result would be C or B)</p>

<p><strong>code</strong></p>

<pre><code>data = json.load(open('j.json'))
x = ""
for item in data["x"]:
    T = True if "A" in item else False
    if T is True:
        x = item["A"]
        #break
    else:
        pass
if x == "":
    print("nothing found")
else :
    print(x)
</code></pre>

<p>when i break it , it just prints B every time i run script
i want it to select B or C random 
any ideas?!</p>

<p>Perhaps this program will answer your (not-yet-asked) question:</p>

<pre><code>import json
import random

data = json.load(open('j.json'))
values = [v for d in data['x'] for k,v in d.items() if k == 'A']
try:
    x = random.choice(values)
    print(x)
except IndexError:
    print("nothing found")
</code></pre>

python json