joshbuddy/jsonpath

Option to symbolize keys while getting the json object at path

Closed this issue · 8 comments

Hi,
Example JSON:
{"store":
{"bicycle":
{"price":19.95, "color":"red"},
"book":[
{"price":8.95, "category":"reference", "title":"Sayings of the Century", "author":"Nigel Rees"},
{"price":12.99, "category":"fiction", "title":"Sword of Honour", "author":"Evelyn Waugh"},
{"price":8.99, "category":"fiction", "isbn":"0-553-21311-3", "title":"Moby Dick", "author":"Herman Melville","color":"blue"},
{"price":22.99, "category":"fiction", "isbn":"0-395-19395-8", "title":"The Lord of the Rings", "author":"Tolkien"}
]
}
}

Path: JsonPath.new('$..book[::2]').on(json)
Current Output: [{"price"=>8.95, "category"=>"reference", "author"=>"Nigel Rees", "title"=>"Sayings of the Century"}, {"price"=>8.99, "category"=>"fiction", "author"=>"Herman Melville", "title"=>"Moby Dick", "isbn"=>"0-553-21311-3"}]

Expected Path: JsonPath.new('$..book[::2]').on(json, options: {:symbolize_keys=> true})
Expected Output: [{:price=>8.95, :category=>"reference", :author=>"Nigel Rees", :title=>"Sayings of the Century"}...]

@heisfullstacked Hi. You should be able to do this with for and a block.

Let me know if that helps.

Hum, that doesn't work for different things. I'll take a look at this when I have some more time on my hand.

Yeah, I don't think I'm going to support this. That would require a massive refactor for which I don't currently have the time or resources.

Sorry buddy. If you have any ideas, please don't hesitate to open a PR.

Hey @joshbuddy! :)

Hum good idea. Probably could do that. But, don't you think that using options like that is weird? I feel like options isn't defined for that purpose. But I might be wrong here? :)

Hey @joshbuddy! :)

Hum good idea. Probably could do that. But, don't you think that using options like that is weird? I feel like options isn't defined for that purpose. But I might be wrong here? :)

Can you explain why it is weird to use options to control the output format? It is more or less similar to telling a method to return a particular file format instead of default one.

¯_(ツ)_/¯ Not sure. Feels weird. I'm going to do it anyways though. :)

This should do the trick. Though I still think this is weird. :)