Skip to content

Commit b297e99

Browse files
committed
Fix bug with extract_data_from_json and jmespath int or float
1 parent f91f4f9 commit b297e99

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

changes/159.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed bug when accessing a jmespath integer or float.

jdiff/extract_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def extract_data_from_json(data: Union[Mapping, List], path: str = "*", exclude:
6262
raise TypeError("JMSPath returned 'None'. Please, verify your JMSPath regex.")
6363

6464
# check for multi-nested lists
65-
if any(isinstance(i, list) for i in values):
65+
if isinstance(values, list) and any(isinstance(i, list) for i in values):
6666
# process elements to check if lists should be flattened
6767
for element in values:
6868
for item in element:

tests/test_get_value.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,16 @@ def test_extract_data_from_json_with_ref_key_and_list_value():
173173
value = extract_data_from_json(data=data, path="[*].[$id$,include_trusted_domains]")
174174

175175
assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value)
176+
177+
178+
def test_extract_data_from_json_with_int_or_float_values():
179+
"""Verify that extract_data_from_json correctly handles ref-key paths when the extracted field value is an int or float."""
180+
data = {"x": 5, "y": 0.75}
181+
182+
expected_value = 5
183+
value = extract_data_from_json(data=data, path="x")
184+
assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value)
185+
186+
expected_value = 0.75
187+
value = extract_data_from_json(data=data, path="y")
188+
assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value)

0 commit comments

Comments
 (0)