I have many JSON objects, which look like below, and what I would like to do, is transform each object, where apart from the small adjustments, the node named last, adds the length nodes, so as in the end, we have the sum of lengths, for each part:
Input sample JSON object
{
"serialnumber": "5690",
"duplicate": true,
"parts": [
{
"serialnumber": "43",
"position_in": true,
"duplicate": true,
"positions": [
{
"self": 0,
"length": 3
},
{
"self": 4,
"length": 1
},
{
"self": 5,
"length": 2
}
]
},
{
"serialnumber": "745",
"position_in": true,
"duplicate": false,
"positions": [
{
"self": 0,
"length": 8
},
{
"self": 8,
"length": 1
},
{
"self": 9,
"length": 1
}
]
}
]
}
Desired JSON output:
{
"5690": {
"duplicate": true,
"parts": {
"43": {
"position_in": true,
"duplicate": true,
"last": "7"
},
"745": {
"position_in": true,
"duplicate": false,
"last": "10"
}
}
}
}
How would you achieve the desired result, using jq, and the + operator?