Closed orhan-cmd closed 1 year ago
@orhan-erday From your example it seems to me that you want to extract the "first" key for each nested object, but the JSON specification states that An object is an unordered set of name/value pairs
(source), so there is no concept of "first" key in an object and it's implementation-dependent.
That said I guess you could leverage the @keys
modifier in a recursive way.
@keys.0
gives you "data"
data.@keys.0
gives you "first"
data.first.@keys.0
gives you "second"
data.first.second.@keys.0
gives you null
that tells you to stop the recursion.In case you know in advance the number of nested layers your object has, then you could leverage the @values
modifier and generate dynamically a multipath query:
[@keys.0,@values.0.@keys.0,@values.0.@values.0.@keys.0]
that would give you:
["data","first","second"]
Thank you so much!
I need to print all first level keys from JSON, how can I do this?
Example:
output
$ data, first, second