Closed haakonstorm closed 2 years ago
I need this feature, too. Let's show a shorter and simpler example. Maybe it's easier to be understood.
My example is, I want to grep the object info whose field retParams
contains a field cover
, but theses objects are arranged in an array. So when I run gron $file | grep "cover"
, I got this.
json.nodes[6].retParams[25] = "cover";
json.nodes[21].retParams[10] = "cover";
json.nodes[22].retParams[1] = "cover";
json.nodes[40].retParams[7] = "cover";
json.nodes[43].retParams[2] = "cover";
json.nodes[94].retParams[26] = "cover";
This output is useless for me. You see index (json.nodes[94]) is large, more than 90
. How could I locate the object in the $file.
I hope, we can display json.nodes[idx].$Attribute
, like the attribute we used is Name
, so the output will be:
Manner 1:
${nodeName2}.retParams[1]="cover"
${nodeName6}.retParams[25]="cover"
...
Manner 2:
Or, we can use other methods, like:
json.nodes[6].Name json.nodes[6].retParams[25] = "cover" ;
json.nodes[21].Name json.nodes[21].retParams[10] = "cover";
...
In this manner, we only prepend each line with a specific attibute value.
JSON
→dot notation
associative array
of sortsI've wanted to request this feature for at least six months now. But, as it is so simple in nature, I primarily looked to solve it with
jq
directly but failed. I resorted to solving it with a two-passgron
job +sed
, but I see now this feature can be of great use to many other use cases.Given the following input JSON
(exerpt snapshot of my
Home Assistant
instance, viahass-cli
):Stuffing it through a shiny
gron
pipe, since it is an array,gron
yields:Essentially, ultimately, what we`re looking for is output like this:
The important change is renaming
json[n]
to the value ofjson[n].entity_id
, which is guaranteed to be unique.If the lone
json[n]
object itself can get the value ofjson[n].state
,gron
does 99% of the job.If
gron
can attempt to flatten the the nested arrays,gron
does 100% of the job:becoming:
gron
somehow needs to know it is getting a (nested) array, and where to get the name of the index.E.g.:
gron --dotnotationableizewith="entity_id" --toplevelvaluesfrom="state" myjson.txt
/mnt/gronfs
.light.bedroom_mini
change fromoff
toon
, a folder hierarchy is created, like the following:/light/bedroom_mini/turn_off.sh
callscurl
with the correct parameters to turn off the light./light/bedroom_mini/turn_off.sh
gets-x
its execute flag removed. If light wasn't turned off viaturn_off.sh
, it first spawns a new subshell and executesturn_off.sh
first./light/bedroom_mini/turn_on.sh
gets+x
execute flag set.last_changed
attribute is stuffed in the file allocation table. So that the folder/light/bedroom_mini
itself gets a new file access modification timestamp.TRUE
to the actual JSON path existing as a file, andFALSE
represented as the file./state
being deleted./person/storm/distance_to.sh
take another path as argument. E.g./person/storm/distance_to.sh "/person/elis"
which looks at$1/{lat, lon}
and$2/{lat, lon}
and echo the distance in meters.This makes it easy to automate stuffs in your life by just writing shell scripts that you can execute arbitrarily. Like turning stuff on and off. But also by appending arbitrary commands to these scripts themselves. E.g.
/light/bedroom_mini/turn_on.sh
can have simple if-else: If my SO + myself aren't home, motion sensor tries to turn on lights, then it was the cat, and he prefer dark mode.What do you think?
This project of mine has actually expanded to something much greater in scope, but this has been long enough already, I'll stop here. Thank you for your great work and success with
gron
!