Home > database >  How to filter for a certain JSON response within 3 iterations
How to filter for a certain JSON response within 3 iterations

Time:07-04

params = {
    'direction': 'desc',
    'page': '1',
    'sort': 'created_at',
    'perPage': '100'
}

response = requests.get('/ledger', params=params, cookies=cookies, headers=headers)

data = response.json()

payouts = [result['category'] for result in data['results']]
final = [result['debitCents'] for result in data['results']]
status = [result['status'] for result in data['results']] 
date = [result['createdAt'] for result in data['results']]

if status == "requested":
    for (x, y, z) in zip(final, date, status):
        print(f'Payout => {x/100:.2f} | Date => {y}  | Status => {z}')
else:
    pass

the JSON had a few possibilities I want to filter through

  1. "status": null
  2. "status": "requested"
  3. "status": "success"

I only want to print the 3 "requested" and I have to loop through each part of the JSON and find that. It won't print but the requests work when I remove the if statement. How can I format this

CodePudding user response:

You didn't provide a sample, but from your code this data structure can be inferred for the received data:

{
  "results": [
    {
      "category": "some category",
      "debitCents": 2,
      "status": null,
      "createdAt": "2022-07-01"
    },
    {
      "category": "some category",
      "debitCents": 3,
      "status": "requested",
      "createdAt": "2022-07-02"
    },
    {
      "category": "some category",
      "debitCents": 4,
      "status": "success",
      "createdAt": "2022-07-03"
    }
  ]
}

You are looping over the data 4 times, to extract 4 lists of values (although you don't use the payouts / category, so I will be ignoring that as well).

However, there's really no need to construct those lists first, and then zip them back together, if all you need is looping through them and using the ones you're interested in (the "requested" ones).

Also, you check for the value of "status" and then expect the zip() to somehow know which tuples to include in the zip and which to ignore, which won't work (how did you expect it to know what to include?)

Here's some code that does what you seem to want to do:

# leaving out the first bit, just assuming you got the data and it is structured as above

for result in data['results']:
    if result["status"] == "requested":
        print(f'Payout => {result["debitCents"]/100:.2f} '
              f'| Date => {result["createdAt"]}' 
              f'| Status => {result["status"]}')

That's all, nothing else needed. If you expected your code to do anything more, you should focus the question on that specifically (or probably write a new, more focused one).

When writing code, only use commands when you know what it is they do, and why they are in the place they are in. If you string along commands that you think 'may help', your code is almost certain to either not work, or have unintended side-effects.

The code I wrote:

  • loops over data['results'], assuming it's some sequence like a list
  • for each result, checks if the status is "requested"
  • and only if that's the case, prints a formatted like of the values of that result

In general, you should include some sample data with your questions on StackOverflow and provide a really clear explanation of what you're getting and what you expected, if you want a maximally useful answer.

  • Related