-
Notifications
You must be signed in to change notification settings - Fork 56
Description
tl;dr: I guess if I'm asking for anything with all the following, it's clarification: is feature-parity with jq an eventual goal for jj, or is it meant to only ever serve for easier access of JSON values in Go app code?
Although tidwall/gjson#161 provided one necessary capability, this kind of task remains impossible with GJSON path syntax and jj. I have a nested structure, but I'm only interested in how many times a certain key has the same value across all objects:
# diagnostics.json
{
"nodes": {
"000-000-0000": {
"node_type": "server"
},
"000-111-0001": {
"node_type": "asav"
},
"222-000-0202": {
"node_type": "server"
},
}
}In other words, how many times is node_type "server", "asav", or any other value which I can't know ahead of time?
With jq, this is fairly straightforward:
jq '.nodes
| [.[].node_type] # Makes an array of all sub-objects' node_type values
| group_by(.) # Sorts the array and groups alike elements into nested arrays
| map( # Makes an object for each nested array like {"key: "server", "value": 2}
{ "key": (.[0]), "value": . | length }
) | from_entries # Turn the arrayed objects into a single object
' diagnostics.json
# {
# "server": 2,
# "asav": 1
# }
With jj, I can get as far as making a list of the node_types with
nodes.@values.#.node_type
# ["server", "asav", "server"]
but there is no way to group or even sort this array for further transformation using just jj.
I understand that GJSON is a Go library and those kinds of operations are, not unreasonably, expected to be implemented in app code. However, the README presents jj as an alternative to jq, which describes itself as "sed for JSON." That's a few steps beyond what is currently possible with jj and GJSON's path syntax, so jj is only a viable alternative for a subset of problems which jq aims to address despite its clear speed advantage.