I would like to produce a list of all values from any given JSON. The special requirement is that for values in nested attributes, all the values of parent’s attributes must be repeated in the line. No keys must be printed.
For example, from this input JSON:
{
"results":[
{
"id":306,
"name":"First Company",
"branches":[
{
"id":4191,
"city":"Seattle",
"customers":[
{
"id":446,
"name":"Big Tech 1"
},
{
"id":447,
"name":"Big Tech 2"
}
]
},
{
"id":4192,
"city":"Oakland",
"customers":[
{
"id":448,
"name":"Health Tech 1"
},
{
"id":449,
"name":"Health Tech 2"
}
]
}
]
}
]
}
I would like to produce this output (please notice the repeated values are highlighted in red, but the output should be with no colors; no keys should be printed):
The JSON above is just an example and generic JSON with arbitrary depth of nesting of JSON objects must be assumed, which suggests that the processing must be recursive. If null appears they must be printed as well.
2
Answers
Iterate and nest:
Demo
To keep the values JSON-encoded (quotes around strings), you could re-convert them using
@json
Demo
Alternatively (to the JSON encoding), you could use the
@csv
builtin, which also escapes strings but also separates the items with a comma:Demo
The following makes various assumptions but only hardcodes "results", "branches" and "customers",
and does not assume that the ordering of the keys within the customer objects is uniform.
Using
scalar_keys
in the obvious way, the solution can also be readily extended to allow for greater depth, andindeed could be generalized to compute the array-valued keys.
This produces arrays, so you will want to tack on a call to
@tsv
or@csv
or similar.