Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 20 additions & 2 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '
{
Expand Down Expand Up @@ -1295,15 +1311,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


Expand Down