Python Forum
how to filter two fields in json using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to filter two fields in json using python
I need some help. I'm trying to filter two fieds in json but its not working. This json is from API and have a lot of pages. In first page its work very weel but when I put this same code in 'While' to get next page, do not work

This is what Im trying to do
results['data'] = [x for x in results['data'] if x['id_employee'] in [14,67] and x['active'] == 'true']
Can someone help and say what Iam doing wrong please
How does it not work? Wrong info or error?

If it were me, I'd break that line up into single actions. Then use print statements to see that all my variables and processes are, and are doing, what I hope.
It's next to impossible to visualize your problem without viewing your code.
JSON can be read into a python dictionary, modified and either re-written or reprocessed as is.

Here's an example that will read and display the contents of a JSON file.
(Works on fairly complex dictionaries, with or without nesting, but not extensively tested,
Also I tested on Linux, but should work on other platforms)
import json
import tkinter.filedialog as fd

class ViewJSON:
    def __init__(self):
        self.jdict = {}

    def display_dict(self, dictname, level=0):
        indent = " " * (4 * level)
        for key, value in dictname.items():
            if isinstance(value, dict):
                level += 1
                self.display_dict(value, level)
                print(f'{indent}{key}: {value}')
            if level > 0:
                level -= 1

    def read_jsonfile(self):
        filename = fd.askopenfilename(defaultextension='.json')
        with open(filename) as fp:
            self.jdict = json.load(fp)

def main():
    vj = ViewJSON()

if __name__ == '__main__':
I'm not getting an error message. The Task works correctly, but it is not filtering the results I need. Returns empty json, like this

My code I posted above. All I want to do is like Where clause with and in SQL.
All employees in (11,22) and active = true.

I don't know the better way to do it in python

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to swap two numbers in fields in python Joni_Engr 5 208 Jan-11-2022, 09:43 AM
Last Post: menator01
  Json fields not being retrieved mrcurious2020 4 955 Sep-14-2020, 06:24 AM
Last Post: bowlofred
  difficulties to chage json data structure using json module in python Sibdar 1 1,053 Apr-03-2020, 06:47 PM
Last Post: micseydel
  json.load() is making json file into string and not python disctionary Heisenberg 2 2,162 Jun-10-2018, 08:29 AM
Last Post: Larz60+
  Excel Filter and copy data by python ls_01 0 2,023 Mar-19-2018, 03:17 PM
Last Post: ls_01
  Mapping Fields zak308 0 1,626 Jan-09-2018, 10:02 PM
Last Post: zak308

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020