From f3392ffd6126a65cc0f66a835018a8f23eba98c3 Mon Sep 17 00:00:00 2001 From: vishnuMohanan01 Date: Sat, 29 Mar 2025 19:30:16 +0530 Subject: [PATCH 1/4] Fix construct_path --- lib/jsonpath.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index 0388f62..a561dd5 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -115,9 +115,11 @@ def self.find_path(obj, root_key, all_paths, is_array = false) def self.construct_path(table_row) if table_row[:index] - return table_row[:root_key] + '['+ table_row[:index].to_s + ']' + table_row[:root_key] + "[" + table_row[:index].to_s + "]" else - return table_row[:root_key] + '.'+ table_row[:key] + table_row_key = table_row[:key].include?(".") ? "'#{table_row[:key]}'" : table_row[:key] + + table_row[:root_key] + "." + table_row_key end end From b7824c305fcc97b140baa51e53e381744d5c7a0e Mon Sep 17 00:00:00 2001 From: vishnuMohanan01 Date: Sat, 29 Mar 2025 19:43:18 +0530 Subject: [PATCH 2/4] Add test --- test/test_jsonpath.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 80fa0bb..53d9136 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -1295,15 +1295,17 @@ def test_fetch_all_path data = { "foo" => nil, "bar" => { - "baz" => nil + "baz" => nil, + "baz.foo" => 3 }, "bars" => [ { "foo" => 12 }, { "foo" => nil }, + { "foo.baz" => 3 }, { } ] } - assert_equal ["$", "$.foo", "$.bar", "$.bar.baz", "$.bars", "$.bars[0].foo", "$.bars[0]", "$.bars[1].foo", "$.bars[1]", "$.bars[2]"], JsonPath.fetch_all_path(data) + assert_equal ["$", "$.foo", "$.bar", "$.bar.baz", "$.bar.'baz.foo'", "$.bars", "$.bars[0].foo", "$.bars[0]", "$.bars[1].foo", "$.bars[1]", "$.bars[2].'foo.baz'", "$.bars[2]", "$.bars[3]"], JsonPath.fetch_all_path(data) end From 826577634180c6a1db026784d5de808b13387358 Mon Sep 17 00:00:00 2001 From: vishnuMohanan01 Date: Sat, 29 Mar 2025 20:03:24 +0530 Subject: [PATCH 3/4] Add test for selecting a key with a dot in it --- test/test_jsonpath.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 53d9136..9f40e10 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -1044,6 +1044,22 @@ def test_selecting_multiple_keys_on_array assert_equal [{ 'category' => 'reference', 'author' => 'Nigel Rees' }, { 'category' => 'fiction', 'author' => 'Evelyn Waugh' }], JsonPath.on(json, '$.store.book[*](category,author)') end + def test_selecting_key_with_a_dot_in_it + json = ' + { + "foo.bar": { + "baz": 3 + }, + "baz.bar": { + "foo.bar": "baz" + } + } + '.to_json + + assert_equal [{ 'baz' => 3 }], JsonPath.on(json, '$.\'foo.bar\'') + assert_equal ["baz"], JsonPath.on(json, '$.\'baz.bar\'.\'foo.bar\'') + end + def test_selecting_multiple_keys_on_array_with_filter json = ' { From 15bd616a6f9a50d70fb7b793a7a8b26e9095ea0d Mon Sep 17 00:00:00 2001 From: vishnuMohanan01 Date: Sat, 29 Mar 2025 20:07:24 +0530 Subject: [PATCH 4/4] String interpolation --- lib/jsonpath.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index a561dd5..78c8a3c 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -115,11 +115,11 @@ def self.find_path(obj, root_key, all_paths, is_array = false) def self.construct_path(table_row) if table_row[:index] - table_row[:root_key] + "[" + table_row[:index].to_s + "]" + "#{table_row[:root_key]}[#{table_row[:index].to_s}]" else table_row_key = table_row[:key].include?(".") ? "'#{table_row[:key]}'" : table_row[:key] - table_row[:root_key] + "." + table_row_key + "#{table_row[:root_key]}.#{table_row_key}" end end