Combine JSON files and recalculate percentages with JQ
In terms of your intermediate results "A" and "B" (denoted below by $a and $b), the following filter produces the required answer. The key is transpose
:
($a | [.hits_percents, .visitors_percents, .bytes_percents] | transpose)| . as $v| reduce range(0; length) as $i ($b; .[$i].hits.percent = $v[$i][0] | .[$i].visitors.percent = $v[$i][1] | .[$i].bytes.percent = $v[$i][2] )
There is probably a better way to get the final result, but you asked how the two intermediate results can be combined....
Bonus question:
# Round a (positive) percentage to two decimal places: def percent: ((1000 * .) | floor) | (. % 10) as $r | ((. / 10) | floor) as $answer | if $r > 4 then ($answer + 1) else $answer end | . / 100 ;
This could be used by adding map(map(percent))
after the transposition:
def percent: ((1000 * .) | floor) | (. % 10) as $r | ((. / 10) | floor) as $answer | if $r > 4 then ($answer + 1) else $answer end | . / 100;($a | [.hits_percents, .visitors_percents, .bytes_percents] | transpose) | map(map(percent)) | . as $v | reduce range(0; length) as $i ($b; .[$i].hits.percent = $v[$i][0] | .[$i].visitors.percent = $v[$i][1] | .[$i].bytes.percent = $v[$i][2] )