Using jsonencode with length 1 array

Viewed 763

When using the MATLAB jsonencode function it seems very difficult to convert size 1 arrays into the correct JSON format i.e. [value]. For example if I do:

jsonencode(struct('words', [string('hello'), string('bye')]))

Then this produces:

{"words":["hello","bye"]}

which is correct. If however I do:

jsonencode(struct('words', [string('hello')]))

Then it produces:

{"words":"hello"}

losing the square brackets, which it needs because it is in general an array. The same happens when using a cell rather than an array, although using a cell does work if it's not inside a struct.

Any idea how I can work around this issue?

1 Answers
Related