Search code examples
jsonpython-3.xurllib

Print All JSON Results if Key Value Equals True


I know this is probably a simple question but I'm a noob and I have spent 3 days searching for the answer to my question with no luck.

Here is a sample of the JSON results I want to search through. I only want to print the JSON results if the key "IsFree" = true.

[
{
"InventorySliceId": 20946771,
"InventoryDateId": 8050,
"StartTime": "2020-10-16T00:00:00",
"UnitId": 5096,
"UnitTypeId": 4303,
"IsPremium": false,
"IsFree": false,
"IsBlocked": false,
"IsReserved": true,
"IsWalkin": false,
"ReservationId": 2651378,
"FacilityId": 377,
"LockExpiration": "0001-01-01T00:00:00",
"IsLocked": false
 },

Honestly I have tried 50 different samples of code and everytime I got errors about the data needs to be an int etc. Here is the current code I put together and it prints nothing.

import json
import urllib.request

# download raw json object
url = 
"https://calirdr.usedirect.com/rdr/rdr/fd/availability
/getbyunit/5096/startdate/2020-10-16/nights/5/true? "

data = urllib.request.urlopen(url).read().decode()

# parse json object
obj = json.loads(data)

for i in obj:
if i['IsFree'] == 'false':
    print(json.dumps(obj, indent=1))

Solution

  • You can try using request's built in json response, which translate it to a valid python dict (or in your case, list of dicts)

    import requests
    from pprint import pprint
    resp = requests.get("https://calirdr.usedirect.com/rdr/rdr/fd/availability/getbyunit/5096/startdate/2020-10-16/nights/5/true?")
    for res in resp.json():
        if res['IsFree'] is False:
            pprint(res)
    

    Result:

    [{'InventorySliceId': 20946771,
      'InventoryDateId': 8050,
      'StartTime': '2020-10-16T00:00:00',
      'UnitId': 5096,
      'UnitTypeId': 4303,
      'IsPremium': False,
      'IsFree': False,
      'IsBlocked': False,
      'IsReserved': True,
      'IsWalkin': False,
      'ReservationId': 2651378,
      'FacilityId': 377,
      'LockExpiration': '0001-01-01T00:00:00',
      'IsLocked': False},
     {'InventorySliceId': 20946968,
      'InventoryDateId': 8049,
      'StartTime': '2020-10-17T00:00:00',
      'UnitId': 5096,
      'UnitTypeId': 4303,
      'IsPremium': False,
      'IsFree': False,
      'IsBlocked': False,
      'IsReserved': True,
      'IsWalkin': False,
      'ReservationId': 2651378,
      'FacilityId': 377,
      'LockExpiration': '0001-01-01T00:00:00',
      'IsLocked': False},
     {'InventorySliceId': 20947072,
      'InventoryDateId': 8052,
      'StartTime': '2020-10-18T00:00:00',
      'UnitId': 5096,
      'UnitTypeId': 4303,
      'IsPremium': False,
      'IsFree': False,
      'IsBlocked': False,
      'IsReserved': True,
      'IsWalkin': False,
      'ReservationId': 2583386,
      'FacilityId': 377,
      'LockExpiration': '0001-01-01T00:00:00',
      'IsLocked': False},
     {'InventorySliceId': 20947253,
      'InventoryDateId': 8051,
      'StartTime': '2020-10-19T00:00:00',
      'UnitId': 5096,
      'UnitTypeId': 4303,
      'IsPremium': False,
      'IsFree': False,
      'IsBlocked': False,
      'IsReserved': True,
      'IsWalkin': False,
      'ReservationId': 2583386,
      'FacilityId': 377,
      'LockExpiration': '0001-01-01T00:00:00',
      'IsLocked': False},
     {'InventorySliceId': 20947341,
      'InventoryDateId': 8053,
      'StartTime': '2020-10-20T00:00:00',
      'UnitId': 5096,
      'UnitTypeId': 4303,
      'IsPremium': False,
      'IsFree': False,
      'IsBlocked': False,
      'IsReserved': True,
      'IsWalkin': False,
      'ReservationId': 2527842,
      'FacilityId': 377,
      'LockExpiration': '0001-01-01T00:00:00',
      'IsLocked': False}]