Show HN

String#to_proc`

by @samleb34

String#to_proc` to Help Traversing JSON I've resuscitated a project I was working on years ago. I was tired of having nested `flat_map` making my code basically unreadable just to fetch a simple value. The idea is to make simple & readable the traversing of deeply nested structures like JSON responses, while staying in Ruby & keeping its flexibility. Here it goes: graphql_response.then(&'[data][repository][issues][edges]*([node][title])') # => ["Support negative indexes", "Document aggregates"] github_prs_response.then(&'*[draft].any?') # => true It's different from jq or JSONPath in that: - it's a Ruby block so you can use it with `map`, `find`, etc. - you can call Ruby method from it - it doesn't automatically flatten arrays, you decide - it raises on `nil` by default (unless you use `&`, i.e. `[customer][address]&[city]`) Obviously, it has drawbacks: - it's yet another syntax to learn - it monkey-patches `String` which might be a blocker for some I'm really looking for feedback here, what do you think? Is it worth releasing a gem with this value proposition?

Discover more builders

Builderlust is an endless, joyful scroll of real projects people are shipping right now. Get the app to keep finding your next spark of inspiration.

📱 Coming soon to iOS & AndroidOpen in the app