Home > Blockchain >  If a value in JSON is 'true' then register the 'id' value
If a value in JSON is 'true' then register the 'id' value

Time:11-20

The JSON template I'm working on looks like this:

enter image description here

My Code:

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}

with open("Live.csv", "w ", newline="", encoding="UTF-8") as f:
    titlemenu = "id"   "\n"
    f.write(titlemenu)
    url = f'https://etcetcetc.com'
    response = requests.get(url, headers=headers).json()
    events = response['events']
    for event in events:
        if 'true' in event['hasEventPlayerStatistics']:
            id = event['id']
            row = str(id)   "\n"
            f.write(row)
    f.close()

The message error is:

TypeError: argument of type 'bool' is not iterable

How should I work this if to be able to retrieve these values?

CodePudding user response:

The issue is here.

if 'true' in event['hasEventPlayerStatistics']:

event['hasEventPlayerStatistics'] is already a python bool object. So you only need to do

if event['hasEventPlayerStatistics']:
      pass # do something

When you do .json call on the response object it already does the following translations.

JSON Python
true True
false False
  • Related