3 ms·
If like me you can do basic queries in jq, but as soon as it gets slightly more complex (e.g. filter objects where some nested property that may not exist has a
by ruuda 1y ago
If like me you can do basic queries in jq, but as soon as it gets slightly more complex (e.g. filter objects where some nested property that may not exist has a particular value), you wish you could do it with familiar Python/Rust/Javascript-like expressions and map/filter, try https://rcl-lang.org/#intuitive-json-queries https://rcl-lang.org/#intuitive-json-queries.
- vidarh 1y agoIf you want it in your actual preferred language, it's usually pretty easy to throw something together. E.g. Ruby: require 'json' script = ARGV.pop $J = JSON.parse(ARGF.read) puts JSON.pretty_generate(eval(script)) Lets you do: curl --silent https://api.vultr.com/v2/regions https://api.vultr.com/v2/regions | ./jr '$J["regions" ].find{_1["id"]=="nrt"}' To translate their example ( curl --silent https://api.vultr.com/v2/regions https://api.vultr.com/v2/regions | rcl query input.regions.key_by(r => r.id).nrt ) (... if you want to commit evil monkey-patching sins: #!/usr/bin/env ruby require 'json' class Hash def method_missing(sym) = (((r = self.dig(sym.to_s)) and return r) or super) end script = ARGV.pop $J = JSON.parse(ARGF.read) script = "$J#{script}" if script.start_with?(".") puts JSON.pretty_generate(eval(script)) Now you can do: curl --silent https://api.vultr.com/v2/regions | ./jr '.regions.find{_1.id =="nrt"}' Or the more direct parallel: curl --silent https://api.vultr.com/v2/regions | ./jr '.regions.group_by{_1.id}.nrt' Since I used ARGF, you either need to quote the script or avoid spaces; you could explicitly use STDIN instead, in which case it's marginally safer to skip the quotes, at the cost of not being able to give a filename as the last argument )