Probably because every call to string(JSON)
re-parses ${json}
.
Instead, you can build up a list of indices to get and fetch them all at once:
set(idxs "")
foreach (idx RANGE ${length})
list(APPEND idxs "${idx}")
endforeach ()
string(JSON names GET "${json}" ${idxs})