diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index 0bd509d..0388f62 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -80,7 +80,7 @@ def join(join_path) def on(obj_or_str, opts = {}) a = enum_on(obj_or_str).to_a - if opts[:symbolize_keys] + if symbolize_keys?(opts) a.map! do |e| e.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; } end @@ -153,4 +153,8 @@ def set_max_nesting return unless @opts[:max_nesting].is_a?(Integer) && @opts[:max_nesting] > MAX_NESTING_ALLOWED @opts[:max_nesting] = false end + + def symbolize_keys?(opts) + opts.fetch(:symbolize_keys, @opts&.dig(:symbolize_keys)) + end end diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 5ab02d7..80fa0bb 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -1312,4 +1312,10 @@ def test_extractore_with_dollar_key assert_equal ["success"], JsonPath.on(json, "$.test.$") assert_equal ["123"], JsonPath.on(json, "$.test.a") end + + def test_symbolize_key + data = { "store" => { "book" => [{"category" => "reference"}]}} + assert_equal [{"category": "reference"}], JsonPath.new('$..book[0]', symbolize_keys: true).on(data) + assert_equal [{"category": "reference"}], JsonPath.new('$..book[0]').on(data, symbolize_keys: true) + end end