filter or delete some dictionaries in a json file

Viewed 59

I am trying to loop through a list of objects. I want to delete or filter {'error': {'code': -32602, 'data': 'invalid account char', 'message': 'Invalid parameters'}, 'id': 1, 'jsonrpc': '2.0'} objects. I tried many ways but none of them worked. Would be grateful if somebody could offer a hand. Thanks.

[{'id': 1,
'jsonrpc': '2.0',
'result': {'account': 'booster',
'follower_count': 16055,
'following_count': 15}},
{'id': 1,
'jsonrpc': '2.0',
'result': {'account': 'bot-helper',
'follower_count': 4286,
'following_count': 5490}},
{'id': 1,
'jsonrpc': '2.0',
'result': {'account': 'cheetah',
'follower_count': 20296,
'following_count': 1}},
{'id': 1,
'jsonrpc': '2.0',
'result': {'account': 'cleverbot',
'follower_count': 3566,
'following_count': 1446}},
{'id': 1,
'jsonrpc': '2.0',
'result': {'account': 'discordia',
'follower_count': 2283,
'following_count': 13}},
{'error': {'code': -32602,
'data': 'invalid account char',
'message': 'Invalid parameters'},
'id': 1,
'jsonrpc': '2.0'},
{'error': {'code': -32602,
'data': 'invalid account char',
'message': 'Invalid parameters'},
'id': 1,
'jsonrpc': '2.0'},
{'error': {'code': -32602,
'data': 'invalid account char',
'message': 'Invalid parameters'},
'id': 1,
'jsonrpc': '2.0'},
3 Answers

See simple function at the beginning of following code, this function filters (removes) error entries. Usage of this function at the end of code.

Try it online!

def Filter(data):
    return [e for e in data if "error" not in e]


data = [
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": {
            "account": "booster",
            "follower_count": 16055,
            "following_count": 15,
        },
    },
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": {
            "account": "bot-helper",
            "follower_count": 4286,
            "following_count": 5490,
        },
    },
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": {
            "account": "cheetah",
            "follower_count": 20296,
            "following_count": 1,
        },
    },
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": {
            "account": "cleverbot",
            "follower_count": 3566,
            "following_count": 1446,
        },
    },
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": {
            "account": "discordia",
            "follower_count": 2283,
            "following_count": 13,
        },
    },
    {
        "error": {
            "code": -32602,
            "data": "invalid account char",
            "message": "Invalid parameters",
        },
        "id": 1,
        "jsonrpc": "2.0",
    },
    {
        "error": {
            "code": -32602,
            "data": "invalid account char",
            "message": "Invalid parameters",
        },
        "id": 1,
        "jsonrpc": "2.0",
    },
    {
        "error": {
            "code": -32602,
            "data": "invalid account char",
            "message": "Invalid parameters",
        },
        "id": 1,
        "jsonrpc": "2.0",
    },
]

print(Filter(data))

Output:

[
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": {
            "account": "booster",
            "follower_count": 16055,
            "following_count": 15,
        },
    },
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": {
            "account": "bot-helper",
            "follower_count": 4286,
            "following_count": 5490,
        },
    },
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": {
            "account": "cheetah",
            "follower_count": 20296,
            "following_count": 1,
        },
    },
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": {
            "account": "cleverbot",
            "follower_count": 3566,
            "following_count": 1446,
        },
    },
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": {
            "account": "discordia",
            "follower_count": 2283,
            "following_count": 13,
        },
    },
]

Filter is a built in function in python. Here is some docs explaining usage.

This should work for your case:

data = [{'id': 1, 'jsonrpc': '2.0'... # Complete this line with your data
filtered_data = filter(lambda x: 'error' not in x, data)
print(filtered_data) # <filter object at 0x10aa164c0>

filter returns a filter object to lazy-load data and conserve memory. It's best to iterate using a for-loop, but you can cast to a list to reveal the results (this uses more memory).

filtered_data = list(filtered_data)
print(filtered_data) # [{'id': 1, 'jsonrpc': '2.0'...

This is written assuming that you only want to filter out that very particular dictionary content, not all error entries.

Just use a list comprehension and remove unwanted dictionaries.

li1 =  [{'id': 1,
    'jsonrpc': '2.0',
    'result': {'account': 'booster',
    'follower_count': 16055,
    'following_count': 15}},
    {'id': 1,
    'jsonrpc': '2.0',
    'result': {'account': 'bot-helper',
    'follower_count': 4286,
    'following_count': 5490}},
    {'id': 1,
    'jsonrpc': '2.0',
    'result': {'account': 'cheetah',
    'follower_count': 20296,
    'following_count': 1}},
    {'id': 1,
    'jsonrpc': '2.0',
    'result': {'account': 'cleverbot',
    'follower_count': 3566,
    'following_count': 1446}},
    {'id': 1,
    'jsonrpc': '2.0',
    'result': {'account': 'discordia',
    'follower_count': 2283,
    'following_count': 13}},
    {'error': {'code': -32602,
    'data': 'invalid account char',
    'message': 'Invalid parameters'},
    'id': 1,
    'jsonrpc': '2.0'},
    {'error': {'code': -32602,
    'data': 'invalid account char',
    'message': 'Invalid parameters'},
    'id': 1,
    'jsonrpc': '2.0'},
    {'error': {'code': -32602,
    'data': 'invalid account char',
    'message': 'Invalid parameters'},
    'id': 1,
    'jsonrpc': '2.0'}
    ]

#my first thought was to use a set, rather than a list, but dicts are unhashables.
#maybe a frozendict? but not sure if they compare equal to normal dicts.  in any #case, you can eliminate more than one type of dict this way. 

unwanted = [{'error': {'code': -32602,
    'data': 'invalid account char',
    'message': 'Invalid parameters'},
    'id': 1,
    'jsonrpc': '2.0'}]

print(f"{len(li1)=}")

li2 = [v for v in li1 if not v in unwanted]

print(f"{len(li2)=}")

output:

len(li1)=8
len(li2)=5

If all you want to is to strip out any error entries, this one liner does it.

li3 = [v for v in li1 if not "error" in v]

output:

len(li3)=5
Related