Skip to content

exclude not working with data lists for extract_data_from_json #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
joewesch opened this issue Apr 9, 2025 · 0 comments · Fixed by #123
Closed

exclude not working with data lists for extract_data_from_json #122

joewesch opened this issue Apr 9, 2025 · 0 comments · Fixed by #123

Comments

@joewesch
Copy link
Contributor

joewesch commented Apr 9, 2025

Environment

  • Python version: 3.11.11
  • jdiff version: 0.0.6

Expected Behavior

I expect to be able to send in a list of dicts for data when using exclude with extract_data_from_json and it properly parses through the list of dicts.

Observed Behavior

exclude only removes keys when data is a dict:

if exclude and isinstance(data, Dict):
if not isinstance(exclude, list):
raise ValueError(f"Exclude list must be defined as a list. You have {type(exclude)}")
# exclude unwanted elements
exclude_filter(data, exclude)

Despite that the underlying exclude_filter works recursively with lists:

elif isinstance(data, list):
for element in data:
if isinstance(element, (dict, list)):
exclude_filter(element, exclude)

Steps to Reproduce

Here, you can see it works when sending in a dict rather than a list:

In [1]: from jdiff import extract_data_from_json

In [2]: data = [{"foo": 1, "bar": 2}]

In [3]: extract_data_from_json(data, exclude=["bar"])
Out[3]: [{'foo': 1, 'bar': 2}]

In [4]: extract_data_from_json(data[0], exclude=["bar"])
Out[4]: {'foo': 1}

But, if you send the list from before to exclude_filter it works as expected:

In [1]: from jdiff.utils.data_normalization import exclude_filter

In [2]: data = [{"foo": 1, "bar": 2}]

In [3]: exclude_filter(data, exclude=["bar"])

In [4]: data
Out[4]: [{'foo': 1}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant