-2

I have a json file like this:

[
    {
        "id": 100,
        "name": "teste",
        "name_test": 3,
        "external_id": "12345",
        "id_job": 14,
        "active": true,
        "email": "[email protected]",
        "phone_country": "55",
        "start_date": "1970-01-01",
        "created_at": "2019-07-15 20:22:44",
        "created_by": 122,
        "created_by_name": "teste"
    },
    {
        "id": 101,
        "name": "teste",
        "name_test": 3,
        "external_id": "12345",
        "id_job": 67,
        "active": true,
        "email": "[email protected]",
        "phone_country": "55",
        "start_date": "1970-01-01",
        "created_at": "2019-07-15 20:22:44",
        "created_by": 122,
        "created_by_name": "teste"
    },
    {
        "id": 102,
        "name": "teste",
        "name_test": 3,
        "external_id": "12345",
        "id_job": 21,
        "active": false,
        "email": "[email protected]",
        "phone_country": "55",
        "start_date": "1970-01-01",
        "created_at": "2019-07-15 20:22:44",
        "created_by": 122,
        "created_by_name": "teste"
    }
]                

I need to filter only "active": true AND "id_job": 14,67

Here is my code I've trying,

result['data'] = [x for x in result['data'] if x['id_job'] in [14,67] and x['active'] == 'true']

The result should be like this

    [
    {
        "id": 100,
        "name": "teste",
        "name_test": 3,
        "external_id": "12345",
        "id_job": 14,
        "active": true,
        "email": "[email protected]",
        "phone_country": "55",
        "start_date": "1970-01-01",
        "created_at": "2019-07-15 20:22:44",
        "created_by": 122,
        "created_by_name": "teste"
    },
    {
        "id": 101,
        "name": "teste",
        "name_test": 3,
        "external_id": "12345",
        "id_job": 67,
        "active": true,
        "email": "[email protected]",
        "phone_country": "55",
        "start_date": "1970-01-01",
        "created_at": "2019-07-15 20:22:44",
        "created_by": 122,
        "created_by_name": "teste"
    }
]                

but as result I receive this

[
    [],
    []
]

My question is similar this one filter json file with python, but I need to put value in the filter

1
  • 4
    true != 'true' Commented Mar 16, 2021 at 13:35

2 Answers 2

1

It's not clear from your question how you're getting data out of your json file and into python so I'm going to make some quick code to do that first, assuming your file is called data.json

import json
with open('data.json') as f:
    data = json.load(f)

data will now be an array that holds dictionary entries.

In your code above you are using the outer most object like a dictionary which is not what the object you gave shows.

Also you're checking for "true" which is a str. When you load this data using the json library it will be the python value of True

So then your filtered data can be created using the following

filtered_data = [x for x in data if x['id_job'] in [14,67] and x['active'] == True]

If you know your json will always have the boolean values of true and false for the active field you can actually simplify the above down to

filtered_data = [x for x in data if x['id_job'] in [14,67] and x['active']]
Sign up to request clarification or add additional context in comments.

1 Comment

oh gosh, it was too close cause I had already tried but as 'true' not True. Its work fine right now. Thank you so much sedavidw
0
result['data'] = [x for x in result['data'] if x['id_job'] in [14,67] and x['active'] == True]

x['active'] is decoded by the json.loads package.

https://docs.python.org/3/library/json.html#json-to-py-table

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.