I have the following JSON array
[
{
"name": "California",
"Data": {
"AB00001": ["Los Angeles", "San Francisco", "Sacramento", "Fresno", "San Jose", "Palo Alto"]
}
},
{
"name": "Oregon",
"Data": {
"CD00002": ["Portland", "Salem", "Hillsboro"]
}
},
{
"name": "Washington",
"Data": {
"EF00003": ["Seattle", "Tacoma", "Spokane", "Bellevue"]
}
}
]
With jq '.[].Data[] | length' I can get the length of each array, but I need to create Length key under Data object and assign to it the length of the array which is in the Data object. The result should look like the following:
[
{
"name": "California",
"Data": {
"ID00001": ["Los Angeles", "San Francisco", "Sacramento", "Fresno", "San Jose", "Palo Alto"],
"Length": 6
}
},
{
"name": "Oregon",
"Data": {
"ID00002": ["Portland", "Salem", "Hillsboro"],
"Length": 3
}
},
{
"name": "Washington",
"Data": {
"ID00003": ["Seattle", "Tacoma", "Spokane", "Bellevue"],
"Length": 4
}
}
]
The problem here is that in my example the object name holding the array (Data in my example) and the array name itself (AB00001/CD00002/EF00003) are not known in advance. However, the values of the name key is known. Also, the array might be empty, so in this case, the Length should be 0.
So the algorithm pseudocode should be either of one as I've envisioned:
for the whole JSON file,
if the type is an array,
then assign it to the Length key created in the parent object of that array,
next
or
For the specific value in the name key, select,
if the entry contains an array
create Length key in the array's parent object,
assign the length of the array,
select the next value of the name key
I tried to use with jq's walk or .. for the first approach but didn't work.
What are the alternatives?