Dynamic generation of rather complex JSON - error of memory allocation for a string

the len value is determined on the first call to vsprintf with a null buffer, then it returns the size of the buffer for the string.

image

unfortunately, I have no idea how to get by with static variables, otherwise I will have to allocate 100500 buffers of 2 kilobytes each, no stack will be enough. in addition, there are also conditions under which it is necessary to use 100% dynamic memory