How to omit fields when using Python JSONDecoder? or Python JSONDecoder omit fields - is there a clea

14 Replies, 1615 Views

You can totally subclass `JSONDecoder` to skip fields! Override the `decode` method and filter out unwanted keys before parsing.

Here's a quick example:

```python
class FilteringDecoder(json.JSONDecoder):
def __init__(self, *args, **kwargs):
self.skip_keys = kwargs.pop('skip_keys', set())
super().__init__(*args, **kwargs)

def decode(self, s):
obj = super().decode(s)
return {k: v for k, v in obj.items() if k not in self.skip_keys}
```

Not the *most* efficient since it still parses everything, but cleaner than post-filtering. For huge JSON, maybe try `ijson` for streaming.

Messages In This Thread



Users browsing this thread: 1 Guest(s)