Stop 'jsonencode' squashing cell arrays?

8 visualizaciones (últimos 30 días)
Arwel
Arwel el 22 de Jul. de 2025
Comentada: Arwel el 23 de Jul. de 2025
Hi,
I've noticed a behaviour of 'jsonencode' (that I don't want), which is to squash cell arrays of doubles into multidimensional arrays when it can. To see this, I made the following example:
clc
% Make up our dummy struct, containing cell arrays of equal size....
myDat.simulation = {rand(10,2) ; rand(10,2)};
myDat.data = {rand(10,3) ; rand(10,3)};
jMyDat = jsondecode(jsonencode(myDat));
myDat
myDat = struct with fields:
simulation: {2×1 cell} data: {2×1 cell}
jMyDat
jMyDat = struct with fields:
simulation: [2×10×2 double] data: [2×10×3 double]
% Make up our dummy struct, containing cell arrays of different size....
myDat2.simulation = {rand(13,2) ; rand(10,2)};
myDat2.data = {rand(13,3) ; rand(10,3)};
jMyDat2 = jsondecode(jsonencode(myDat2));
myDat2
myDat2 = struct with fields:
simulation: {2×1 cell} data: {2×1 cell}
jMyDat2
jMyDat2 = struct with fields:
simulation: {2×1 cell} data: {2×1 cell}
..in other words, 'jsonencode' collapses the cell arrays into a single 3D array is the arrays are the same size, but won't if they are different.
The trouble is that I need to preserve the cell arrays for downstream stuff, so the second behaviour(i.e. not collapsing the cells) is the one I want.
Is there a way to force jsonencode to preserve the cell arrays is all cases?
Cheers,
Arwel

Respuesta aceptada

Stephen23
Stephen23 el 22 de Jul. de 2025
Editada: Stephen23 el 22 de Jul. de 2025
"Is there a way to force jsonencode to preserve the cell arrays is all cases?"
No, the JSON standard does not have cell arrays. Nor does it have numeric arrays. Like many programming languages it uses nested lists (called "array" by JSON), so that is exactly what a cell array of numeric matrices get converted to. And JSON does not say anything about how those nested lists should be interpreted, but it seems that MATLAB uses the fairly common interpretation of as a single numeric matrix/array.
The conversion to nested lists is inherently lossy: there is no way to then distinguish between different MATLAB data types (and their combinations) that might all get converted to the same nested lists:
jsonencode(struct('x',[1,2,4])) % numeric vector
ans = '{"x":[1,2,4]}'
jsonencode(struct('x',{{1,2,4}})) % cell array
ans = '{"x":[1,2,4]}'
C'est la vie.
This is also clearly stated in the JSONENCODE documentation:
  • "If you encode, then decode a value, MATLAB does not guarantee that the data type is preserved. JSON supports fewer data types than MATLAB, which results in loss of type information."
  • "MATLAB does not guarantee that the shape of an array is preserved. For example, a 1-by-N numeric vector is encoded as an array. If you call jsondecode, then MATLAB decodes the array as an N-by-1 vector."
Workaround: if you never store multi-dimensional arrays then you could add some simple post-processing to convert them back into a cell array of numeric matrices. Or you could add a flag that tells you to split the multi-dimensional array.
  1 comentario
Arwel
Arwel el 23 de Jul. de 2025
Hi Stephen.... Thanks for your detailed answer..
"you could add some simple post-processing to convert them back into a cell array of numeric matrices" - yes, that's exactly what I've ended up doing!

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by