Using an IF THEN loop with nested JSON files in Python
<p>I am currently writing a program which uses the ComapaniesHouse API to return a json file containing information about a certain company. </p>

<p>I am able to retrieve the data easily using the following commands:</p>

<pre><code>r = requests.get('https://api.companieshouse.gov.uk/company/COMPANY-NO/filing-history', auth=('API-KEY', ''))

data = r.json()
</code></pre>

<p>With that information I can do an awful lot, however I've ran into a problem which I was hoping you guys could possible help me with. What I aim to do is go through every nested entry in the json file and check if the value of certain keys matches certain criteria, if the values of 2 keys match a certain  criteria then other code is executed. </p>

<p>One of the keys is the date of an entry, and I would like to ignore results that are older than a certain date, I have attempted to do this with the following:</p>

<pre><code>date_threshold = datetime.date.today() - datetime.timedelta(days=30)``



for each in data["items"]:
        date = ['date']
        type = ['type']
     if date &lt; date_threshold and type is "RM01":
        print("wwwwww")
</code></pre>

<p>In case it isn't clear, what I'm attempting to do (albeit very badly) is assign each of the entries to a variable, which then gets tested against certain criteria.</p>

<p>Although this doesn't work, python spits out a variable mismatch error:</p>

<pre><code>TypeError: unorderable types: list() &lt; datetime.date()
</code></pre>

<p>Which makes me think the date is being stored as a string, and so I can't compare it to the datetime value set earlier, but when I check the API documentation (<a href="https://developer.companieshouse.gov.uk/api/docs/company/company_number/filing-history/filingHistoryItem-resource.html" rel="nofollow">https://developer.companieshouse.gov.uk/api/docs/company/company_number/filing-history/filingHistoryItem-resource.html</a>), it says clearly that the 'date' entry is returned as a date type.</p>

<p>What am I doing wrong, its very clear that I'm extremely new to python given what I presume is the atrocity of my code, but in my head it seems to make at least a little sense. In case none of this clear, I basically want to go through all the entries in the json file, and the if the date and type match a certain description, then other code can be executed (in this case I have just used random text).</p>

<p>Any help is greatly appreciated! Let me know if you need anything cleared up.</p>

<p>:)</p>

<p>EDIT</p>

<p>After tweaking my code to the below:</p>

<pre><code>for each in data["items"]:
        date = each['date']
        type = each['type']
     if date is '2016-09-15' and type is "RM01":
        print("wwwwww")
</code></pre>

<p>The code executes without any errors, but the words aren't printed, even though I know there is an entry in the json file with that exact date, and that exact type, any thoughts?</p>

<p>SOLUTION:</p>

<p>Thanks to everyone for helping me out, I had made a couple of very basic errors, the code that works as expected is below::</p>

<pre><code>for each in data["items"]:
    date = each['date']
    typevariable = each['type']

    if date == '2016-09-15' and typevariable == "RM01":
        print("wwwwww")
</code></pre>

<p>This prints the word "wwwwww" 3 times, which is correct seeing as there are 3 entries in the JSON that fulfil those criteria.</p>

<p>You need to first convert your date variable to a datetime type using <a href="https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime" rel="nofollow">datetime.strptime()</a></p>

<p>You are comparing a list type variable <code>date</code> with datetime type variable <code>date_threshold</code>.</p>

python json python-3.x