MATLAB: How do you jsondecode or display UTF-8 bytestrings

jsondecodeMATLABsprintfutf-8

How do you jsondecode or display UTF-8 bytestrings?

Best Answer

Currently jsondecode does not support decoding utf-8 encoded bytestrings. Calling jsondecode on such strings throws an error as below:
>> jsontext = '[{"ID":"\xce\xa3"}]'
jsontext =
'[{"ID":"\xce\xa3"}]'
>> jsondecode(jsontext)
Error using jsondecode
JSON syntax error at line 1, column 9 (character 9): escape character '\x' is not valid.
To use jsondecode the \x needs to be removed using sprintf
>> jsondecode(sprintf(jsontext))
ans =
struct with fields:
ID: 'Σ'
However this is not the desired output as '\xce\xa3' represents the sigma symbol in utf-8.
A workaround was found by manually parsing the byte string and generating the display symbol using the attached "parseBytes" function:
>> ans.ID = parseBytes("\xce\xa3");
>> disp(ans)
ID: 'Σ'
This displays the appropriate sigma symbol.