Parsing through JSON file with python and selecting multiple values on certain conditions
<p>I have this JSON file. </p>

<pre><code>{  
   "reviewers":[  
      {  
         "user":{  
            "name":"keyname",
            "emailAddress":"John@email",
            "id":3821,
            "displayName":"John Doe",
            "active":true,
            "slug":"jslug",
            "type":"NORMAL",
            "link":{  
               "url":"/users/John",
               "rel":"self"
            },
         },
         "role":"REVIEWER",
         "approved":true
      },
      {  
         "user":{  
            "name":"keyname2",
            "emailAddress":"Harry@email",
            "id":6306,
            "displayName":"Harry Smith",
            "active":true,
            "slug":"slug2",
            "link":{  
            "type":"NORMAL",
               "url":"/users/Harry",
               "rel":"self"
            },
         },
         "role":"REVIEWER",
         "approved":false
      }
   ],
}
</code></pre>

<p>Initially, I was using a snippet of code that would go through and grab the full names of the reviewers. </p>

<pre><code>def get_reviewers(json):
    reviewers = ""
    for key in json["reviewers"]:
        reviewers += key["user"]["displayName"] + ", "
    reviewers = reviewers[:-2]
    return reviewers
</code></pre>

<p>which would return <code>"John Doe, Harry Smith"</code>. However, now I'm trying to get it so that the script will return a (A) next to the name of the user if their tag equals true <code>"approved"=true</code>.</p>

<p>So for example the code above would get the names, then see that John's approved tag is true and Harry's is false, then return <code>"John Doe(A), Harry Smith"</code>. I'm just not sure where to even begin to do this. Can anyone point me in the right direction?</p>

<p>This is what I've been trying so far but obviously it isn't working as I'd like it to. </p>

<pre><code>def get_reviewers(stash_json):
    reviewers = ""
    for key in stash_json["reviewers"]:
        if stash_json["reviewers"][0]["approved"] == true:
            reviewers += key["user"]["displayName"] + "(A)" + ", "
        else:
            reviewers += key["user"]["displayName"] + ", "
    reviewers = reviewers[:-2]
    return reviewers
</code></pre>

<p>which outputs <code>Jason Healy(A), Joan Reyes(A)</code></p>

<p>This is what my <code>stash_json</code> outputs when put through pprint. </p>

<p>You probably want something along the lines of this:</p>

<pre><code>def get_reviewers(stash_json):
    reviewers = ""
    for item in stash_json["reviewers"]:
        if item["approved"]:
            reviewers += item["user"]["displayName"] + "(A)" + ", "
        else:
            reviewers += item["user"]["displayName"] + ", "
    reviewers = reviewers[:-2]
    return reviewers
</code></pre>

<p>I think part of your confusion comes from the fact that "reviewers" is a <code>list</code> of <code>dict</code> elements, and each <code>dict</code> element has a key-value <code>approved</code>, but also a key "user" which value itself is another <code>dict</code>.</p>

<hr>

<p>Read the JSON file carefully, and for debugging purposes, use plenty of</p>

<pre><code>print(...)
print(type(...))   # whether something is a dict, list, str, bool etc
</code></pre>

<p>or</p>

<pre><code>from pprint import pprint    # pretty printing
pprint(...)
</code></pre>

python json