7 ms·
So I guess we all know https://jsonnet.org https://jsonnet.org, right? It's advertised as a data templating language. Interestingly enough, you can also use it
by EdSchouten 7y ago
So I guess we all know https://jsonnet.org https://jsonnet.org, right? It's advertised as a data templating language. Interestingly enough, you can also use it as a querying tool. The first four examples on the jql page can be translated to Jsonnet as follows:
$ jsonnet -e "(import 'test.json').countries[0]"
$ jsonnet -e "(import 'test.json').countries[0:2]"
$ jsonnet -e "[x.name for x in (import 'test.json').countries]"
$ jsonnet -e "std.objectFields((import 'test.json').countries[0])"
With that in mind, I never bothered to learn how to use a tool like jq, rq, jql, etc.
- cube2222 7y agoThis is actually the best alternative to jq for me I've seen so far! (other than jql obviously ;) ) Thanks for posting!
- hnlmorg 7y agoShameless plug but I've also written a language (murex) for shell scripting which can be used for this too: open test.json -> [ countries ] open test.json -> [[ /countries/0 ]] open test.json -> [ countries ] -> [ 0 2 ] Single brackets only return the next level deep but can retrieve multiple items (and negative integers to count from the end of an array). Double brackets are to specify a path. So [[/foo/bar]] is literally the same as [foo]->[bar] Lastly the 4th example: open test.json -> [ countries ] -> foreach c { echo $c[name] } Unfortunately this one doesn't output it in JSON. If you needed that you could chain another command to reformat it: open test.json -> [ countries ] -> foreach c { echo $c[name] } -> cast str -> format json This works because murex can auto-convert between lists, JSON, YAML, CSV and a few other structured formats. However it's fair to say it is a lot more verbose than jsonnet and jql in that last example. Github repo: https://github.com/lmorg/murex https://github.com/lmorg/murex I've been using this as my primary shell for a few years now and, like yourself, I've never bothered to learn jq because of that. NB all of the above example are running from inside the murex interactive command line (like bash). If you wanted to run it like jq/jql then you'd need to do the same sort of thing as bash: murex -c 'open test.json -> [ countries ]'
- jaib8 7y agoI haven't used jsonnet before but is it possible to read via stdin? for eg to parse a json response from a rest endpoint in jq I would do something like: $ curl "..." | jq .abc
- FragenAntworten 7y agoAnother option which looks interesting: https://github.com/jmespath/jp https://github.com/jmespath/jp I haven't used it yet, but the syntax seems simpler than some.
- edaemon 7y agoThanks for the link, this straightforward syntax is what I've been after for dealing with JSON on the command line.
- theamk 7y ago> Language Reference > TODO: This page still needs to be written. Looks like it is not ready for prime time yet... (Not to mention that it seems to have no support for stdin input, multiple input files, and json lines format - which are my main uses cases for jq)
- ljm 7y agoMy favourite is yq, because there are two competing and utterly incompatible implementations of it. Just to add onto the shitshow that YAML can be.
- Maxious 7y agobrew install yq vs. brew install python-yq always gets me
- paulddraper 7y agojsonnet is more verbose (especially with that import syntax), but scales well, with separate files, functions, etc.
- krick 7y agoWow. No, we all didn't know jsonner.org, and, TBH, it was always a problem for me to remember jq syntax every time I needed to do something. This really looks perfect, thanks!
- iudqnolq 7y agoWow! Thank you so much for showing me this. I never before wanted to write object oriented JSON, but now my mind has been opened. For those who haven't yet seen it/opened the link, it's 20% project that describes itself as: > A data templating language for app and tool developers - Generate config data - Side-effect free - Organize, simplify, unify - Manage sprawling config > A simple extension of JSON - Open source (Apache 2.0) - Familiar syntax - Reformatter, linter, editor & IDE integrations - Formally specified
- EdSchouten 7y agoYeah. It’s a pretty sweet tool. What’s also pretty awesome about it is that its interpreters (one C++ and one Go implementation) can also easily be embedded into custom applications. For an Open Source project of mine (https://github.com/buildbarn https://github.com/buildbarn, a remote build cluster implementation for the Bazel build system), we are using go-jsonnet as the config file parser library. This means that people can use JSON, but if they need something that’s more flexible, they can add Jsonnet statements immediately. It’s also possible to have that as a preprocessing stage (init scripts), but in a containerized world it’s easier to have it embedded. One of the nice things about doing that is that I also don’t need to write code to load TLS certificate data from disk. People can either embed their secrets directly into the config file, or they can use Jsonnet’s ‘importstr’ keyword to load separate PEM files from disk. Somewhat related to that: Jsonnet also allows people to write libraries for constructing more complex JSON files. For example, the Grafana folks have released a library (https://github.com/grafana/grafonnet-lib https://github.com/grafana/grafonnet-lib) that allows you to easily build Grafana dashboards from code.
- iudqnolq 7y ago> This means that people can use JSON, but if they need something that’s more flexible, they can add Jsonnet statements immediately. That sounds awesome. I'd expect it's worth it just to have self and $. And not only that, jsonnet can also produce yaml and ini files. I might try using it to configure my whole machine...