Comments (7)
I have something that may help in my .bashrc
:
#get all paths of a json
jqpath()
{
jq -r 'def path2text($value):
def tos: if type == "number" then . else tojson end;
reduce .[] as $segment (""; .
+ ($segment
| if type == "string" then "." + . else "[\(.)]" end))
+ " = \($value | tos)";
paths(scalars) as $p
| getpath($p) as $v
| $p | path2text($v)' "$@"
}
jqpath2()
{
jq -rc 'path(..)|[.[]|tostring]|join("/")' "$@"
#jq -r '[path(..)|map(if type=="number" then "[]" else tostring end)|join(".")|split(".[]")|join("[]")]|unique|map("."+.)|.[]' "$@"
}
#https://github.com/stedolan/jq/issues/243
#also see `gron' package to flatten json
#https://www.datafix.com.au/BASHing/2022-03-23.html
from jq.
@wader, didn't know about gron, thanks for sharing.
I was thinking this would be much easier. When -p is present, the -r would be implied and the expression mentioned above would be used. Also, I just updated the issue to state that the output jsonpath format should be presentable as input.
from jq.
Hi, interesting idea. I wonder if it could be modelled as another input and output format (#467)? thinking this basically is the gron "format"? example:
$ jq -n '{a:"a",b:[1]}' | gron
json = {};
json.a = "a";
json.b = [];
json.b[0] = 1;
$ jq -n '{a:"a",b:[1]}' | gron | gron -u
{
"a": "a",
"b": [
1
]
}
I think there also has been some discussion, that i can't find now, about adding some helpers for dealing paths. That would at least simplify doing things like this a bit, for example fq has path_to_expr
and expr_to_path
:
$ fq -cn '".a[0]" | expr_to_path | ., path_to_expr'
["a",0]
".a[0]"
from jq.
Agree on output should be in input format, maybe should even be <path> = <value>
to be valid jq? not sure what i think about -p
yet, have to think about it. It would also be great with input from other jq maintainers on this but it might take a while.
from jq.
@wader wrote:
It would also be great with input from other jq maintainers
The jq authors and maintainers seem to be in pretty close agreement that new command-line options are strenuously to be avoided, especially when a straightforward jq program can fill the bill.
In this particular case, the existing --stream
option does seem to meet the stated functional requirements. In fact, the JSON stream produced by --stream can be used programmatically even more easily that the proposed output, the point being that the output interfaces nicely with jq's setpath
.
Please also note even if the output produced by --stream -c
is itself deemed unsatisfactory, it can nevertheless
very easily and succinctly be tweaked to achieve variants such as the one that has been proposed.
from jq.
@wader wrote:
It would also be great with input from other jq maintainers
The jq authors and maintainers seem to be in pretty close agreement that new command-line options are strenuously to be avoided, especially when a straightforward jq program can fill the bill.
Agreed
In this particular case, the existing
--stream
option does seem to meet the stated functional requirements. In fact, the JSON stream produced by --stream can be used programmatically even more easily that the proposed output, the point being that the output interfaces nicely with jq'ssetpath
.Please also note even if the output produced by
--stream -c
is itself deemed unsatisfactory, it can nevertheless very easily and succinctly be tweaked to achieve variants such as the one that has been proposed.
Good point, and it's way more readable then i expected 👍
$ pbpaste | jq --stream -c 'select(length==2)'
[["Reservations",0,"Groups"],[]]
[["Reservations",0,"Instances",0,"ImageId"],"ami-a"]
[["Reservations",0,"Instances",0,"InstanceId"],"i-a"]
[["Reservations",0,"Instances",0,"InstanceType"],"t2.micro"]
[["Reservations",0,"Instances",0,"KeyName"],"ubuntu"]
from jq.
...that's a step in the right direction, thanks for putting that together.
from jq.
Related Issues (20)
- The jq returned wrong json after updating based on select. HOT 1
- capture returns inconsistent results for optional named group HOT 2
- Please add nullish coalescing operator "??" to JQ? HOT 3
- Cannot select sub-field that has a dash in the name of the field HOT 1
- compile error HOT 10
- Multiplying -1 with 0 must result 0, not -0 HOT 2
- ~/.jq is not sourced on windows
- Unexpected output HOT 1
- Null bytes are handled inconsistently HOT 5
- tonumber doesn't work on `true` or `false` HOT 4
- "color for object keys" from JQ_COLORS doesn't seem to be respected HOT 4
- `range/3` behaviour when $init and $upto arguments are not numbers HOT 4
- Regular expression alternation (|) used with quantifier (* or +) returns inconsistent results when first alternative is able to match an empty string HOT 3
- Create Multi Smaller Files From a Big Json File HOT 5
- Add the Symbolic Binding Operator ("as") to the operator priority table HOT 1
- Including and importing module with importing JSON crashes jq
- The code example for trim ltrim and rtrim from the manual do not work HOT 2
- Incorrect `Invalid path expression` in destructuring expressions
- 1.7.1 dumps core on Cygwin, trying to find the input filename to print an error message HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jq.