diff --git a/changes/159.fixed b/changes/159.fixed new file mode 100644 index 0000000..c4fc890 --- /dev/null +++ b/changes/159.fixed @@ -0,0 +1 @@ +Fixed bug when accessing a jmespath integer or float. diff --git a/jdiff/extract_data.py b/jdiff/extract_data.py index 9190401..bcb1965 100644 --- a/jdiff/extract_data.py +++ b/jdiff/extract_data.py @@ -62,7 +62,7 @@ def extract_data_from_json(data: Union[Mapping, List], path: str = "*", exclude: raise TypeError("JMSPath returned 'None'. Please, verify your JMSPath regex.") # check for multi-nested lists - if any(isinstance(i, list) for i in values): + if isinstance(values, list) and any(isinstance(i, list) for i in values): # process elements to check if lists should be flattened for element in values: for item in element: diff --git a/tests/test_get_value.py b/tests/test_get_value.py index c185f28..3a5a1dd 100644 --- a/tests/test_get_value.py +++ b/tests/test_get_value.py @@ -173,3 +173,16 @@ def test_extract_data_from_json_with_ref_key_and_list_value(): value = extract_data_from_json(data=data, path="[*].[$id$,include_trusted_domains]") assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value) + + +def test_extract_data_from_json_with_int_or_float_values(): + """Verify that extract_data_from_json correctly handles ref-key paths when the extracted field value is an int or float.""" + data = {"x": 5, "y": 0.75} + + expected_value = 5 + value = extract_data_from_json(data=data, path="x") + assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value) + + expected_value = 0.75 + value = extract_data_from_json(data=data, path="y") + assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value)