- "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."
Stop 'jsonencode' squashing cell arrays?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
jMyDat
% 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
jMyDat2
..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
0 comentarios
Respuesta aceptada
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
jsonencode(struct('x',{{1,2,4}})) % cell array
C'est la vie.
This is also clearly stated in the JSONENCODE documentation:
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre JSON Format en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!