I would like to produce a list of all unique key path from a given JSON, each one in a line.
For example, from this input JSON:
{
"results":[
{
"id":306,
"name":"First Company",
"branches":[
{
"id":4191,
"city":"San Francisco",
"customers":[
{
"id":446,
"name":"Big Tech 1"
},
{
"id":447,
"name":"Big Tech 2"
}
]
},
{
"id":4192,
"city":"Miami",
"customers":[
{
"id":448,
"name":"Insurance Tech 1"
},
{
"id":449,
"name":"Health Tech 2"
}
]
}
]
}
]
}
I would like to produce this output:
results
results.id
results.name
results.branches
results.branches.id
results.branches.city
results.branches.customers
results.branches.customers.id
results.branches.customers.name
I am struggling with the command line below but it is not working.
jq '
def path(k):
if k | type == "object":
reduce(.keys[] | path(.), k)
else:
[k]
;
.[] | path(".") | flatten | sort' example.json
Any help, please?
With
pathsfunction, filtering out array indices of object keys and collecting unique paths: