Skip to content

Commit c759c33

Browse files
committed
Bugfix to data normalization
1 parent 4a423a7 commit c759c33

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

jdiff/extract_data.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,25 @@ def extract_data_from_json(data: Union[Mapping, List], path: str = "*", exclude:
4848
if values is None:
4949
raise TypeError("JMSPath returned 'None'. Please, verify your JMSPath regex.")
5050

51-
# check for multi-nested lists if not found return here
52-
if not any(isinstance(i, list) for i in values):
53-
return values
54-
55-
# process elements to check if lists should be flattened
56-
for element in values:
57-
for item in element:
58-
# raise if there is a dict, path must be more specific to extract data
59-
if isinstance(item, dict):
60-
raise TypeError(
61-
f'Must be list of lists i.e. [["Idle", 75759616], ["Idle", 75759620]]. You have "{values}".'
62-
)
63-
if isinstance(item, list):
64-
values = flatten_list(values) # flatten list and rewrite values
65-
break # items are the same, need to check only first to see if this is a nested list
66-
67-
paired_key_value = associate_key_of_my_value(jmespath_value_parser(path), values)
51+
# check for multi-nested lists
52+
if any(isinstance(i, list) for i in values):
53+
# process elements to check if lists should be flattened
54+
for element in values:
55+
for item in element:
56+
# raise if there is a dict, path must be more specific to extract data
57+
if isinstance(item, dict):
58+
raise TypeError(
59+
f'Must be list of lists i.e. [["Idle", 75759616], ["Idle", 75759620]]. You have "{values}".'
60+
)
61+
if isinstance(item, list):
62+
values = flatten_list(values) # flatten list and rewrite values
63+
break # items are the same, need to check only first to see if this is a nested list
6864

6965
# We need to get a list of reference keys - list of strings.
7066
# Based on the expression or data we might have different data types
7167
# therefore we need to normalize.
7268
if re.search(r"\$.*\$", path):
69+
paired_key_value = associate_key_of_my_value(jmespath_value_parser(path), values)
7370
wanted_reference_keys = jmespath.search(jmespath_refkey_parser(path), data)
7471

7572
if isinstance(wanted_reference_keys, dict): # when wanted_reference_keys is dict() type

0 commit comments

Comments
 (0)