|
1 | 1 | """Extract data from JSON. Based on custom JMSPath implementation.""" |
2 | 2 | import re |
3 | 3 | import warnings |
4 | | -from typing import Mapping, List, Dict, Any, Union |
| 4 | +from typing import Mapping, List, Dict, Any, Union, Optional |
5 | 5 | import jmespath |
6 | 6 | from .utils.data_normalization import exclude_filter, flatten_list |
7 | 7 | from .utils.jmespath_parsers import ( |
|
12 | 12 | ) |
13 | 13 |
|
14 | 14 |
|
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: |
16 | 16 | """Return wanted data from outpdevice data based on the check path. See unit test for complete example. |
17 | 17 |
|
18 | 18 | 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: |
48 | 48 | if values is None: |
49 | 49 | raise TypeError("JMSPath returned 'None'. Please, verify your JMSPath regex.") |
50 | 50 |
|
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 |
68 | 64 |
|
69 | 65 | # We need to get a list of reference keys - list of strings. |
70 | 66 | # Based on the expression or data we might have different data types |
71 | 67 | # therefore we need to normalize. |
72 | 68 | if re.search(r"\$.*\$", path): |
| 69 | + paired_key_value = associate_key_of_my_value(jmespath_value_parser(path), values) |
73 | 70 | wanted_reference_keys = jmespath.search(jmespath_refkey_parser(path), data) |
74 | 71 |
|
75 | 72 | if isinstance(wanted_reference_keys, dict): # when wanted_reference_keys is dict() type |
|
0 commit comments