Skip to content

exclude not working with data lists for extract_data_from_json #122

@joewesch

Description

@joewesch

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}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions