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
#1
Hi
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
Reply
#2
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.
Reply
#3
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):
                print(f'\n{indent}{key}')
                level += 1
                self.display_dict(value, level)
            else:
                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()
    vj.read_jsonfile()
    vj.display_dict(vj.jdict)


if __name__ == '__main__':
    main()
Reply
#4
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

[
[],
[]
]
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add two resultant fields from python lambda function klllmmm 4 900 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  Json filter is not capturing desired key/element mrapple2020 1 1,115 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  Python Split json into separate json based on node value CzarR 1 5,566 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  Compare fields from two csv files georgebijum 3 1,358 Apr-25-2022, 11:16 PM
Last Post: Pedroski55
  How to swap two numbers in fields in python Joni_Engr 5 1,836 Jan-11-2022, 09:43 AM
Last Post: menator01
  Json fields not being retrieved mrcurious2020 4 2,039 Sep-14-2020, 06:24 AM
Last Post: bowlofred
  difficulties to chage json data structure using json module in python Sibdar 1 2,075 Apr-03-2020, 06:47 PM
Last Post: micseydel
  Excel Filter and copy data by python ls_01 0 2,852 Mar-19-2018, 03:17 PM
Last Post: ls_01
  Mapping Fields zak308 0 2,487 Jan-09-2018, 10:02 PM
Last Post: zak308

Forum Jump:

User Panel Messages

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