Skip to content

Commit d30d359

Browse files
authored
Merge pull request #96 from networktocode/bugfix
Bugfix to ref_key and data normalization.
2 parents 21428e8 + c759c33 commit d30d359

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

jdiff/extract_data.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Extract data from JSON. Based on custom JMSPath implementation."""
22
import re
33
import warnings
4-
from typing import Mapping, List, Dict, Any, Union
4+
from typing import Mapping, List, Dict, Any, Union, Optional
55
import jmespath
66
from .utils.data_normalization import exclude_filter, flatten_list
77
from .utils.jmespath_parsers import (
@@ -12,7 +12,7 @@
1212
)
1313

1414

15-
def extract_data_from_json(data: Union[Mapping, List], path: str = "*", exclude: List = None) -> Any:
15+
def extract_data_from_json(data: Union[Mapping, List], path: str = "*", exclude: Optional[List] = None) -> Any:
1616
"""Return wanted data from outpdevice data based on the check path. See unit test for complete example.
1717
1818
Get the wanted values to be evaluated if JMESPath expression is defined,
@@ -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)