Hi,
I'm trying to achieve the following: Dump paths to leafs and the leaf values.
Example:
$ echo '{"a":{"b":1,"c":2},"d":["x",1]}' | jq '
a.b = 1
a.c = 2
d.0 = x
d.1 = 1
I think using the recurse-function it should be possible somehow, any ideas?
It is a bit similar to what
https://github.com/wuyongzheng/json-liner
does, but the output format is more or less fixed there. And using jq I could apply all of jq's filtering possibilities.
Regards,
Tilo
def r(p):.as$in| if .>=[] then keys[]| .as$k| $in[.]| r(p+".\($k)") else p+" = \(.)" end; r("")
Nice!
This needs magic though. That expression, and simplified versions, are too cumbersome. I opened #97 which I see now is a dup of this one :(
Here's a not-too-magical version. First you define a function to get all of the leaves of the structure:
def leaves: if type == "array" or type == "object" then .[] | leaves else . end;
Then, you can ask jq for all of the paths that this function traverses:
path(leaves)
The output is:
["d",0]
["d",1]
["a","c"]
["a","b"]
You can use getpath to retrieve the value at a particular path:
path(leaves) as $p | [$p, getpath($p)]
produces:
[["d",0],"x"]
[["d",1],1]
[["a","c"],2]
[["a","b"],1]
Converting these into a readable non-JSON format is still a bit of a pain since jq's string manipulation is weak, but that's a separate issue.
Why does it need $p? i.e. this doesn't work: path(leaves) | [., getpath(.)]
You're calling getpath passing the path as input. getpath expects the object to inspect as input and the path as the first argument.
Thanks; my silly error.
You can also use paths(scalars) instead of path(leaves) to get a version without custom functions:
paths(scalars) as $p | [$p, getpath($p)]
As far as I can tell, it鈥檚 equivalent to @stedolan鈥檚 suggestion.
Hi, maybe it's silly to ask on such an old issue, but is there a reason getpath is not documented?
getpath is documented in the manual at
https://stedolan.github.io/jq/manual/
it was not documented in earlier versions, perhaps because it was originally viewed as being subject to change.
Most helpful comment
You can also use
paths(scalars)instead ofpath(leaves)to get a version without custom functions:As far as I can tell, it鈥檚 equivalent to @stedolan鈥檚 suggestion.