MATLAB: Opening .json file

charjsonjsondecoderMATLABsyntax error

Hi there, I am new at working with .json files and I am trying to open the data from an IMU. I using the code below, however, when using the jsondecode function I keep getting the following error "Error using jsoncode JSON syntaz error at line 2, column 1 (character 634) extra text". The line and column and character differ depending on the file, but cannot find what ios the "extra text". Any ideas how to solve the problem?
many thanks
eduardo
fileName = '20201108T225105Z_180230000291_gyro_stream.json'; % filename in JSON extension
fid = fopen(fileName); % Opening the file
raw = fread(fid,inf); % Reading the contents
str = char(raw'); % Transformation
fclose(fid); % Closing the file
data = jsondecode(str); % Using the jsondecode function to parse JSON from string

Best Answer

The most common cause for the extra text error is additional “” signs in the json string. Please check the formatting of the jsoncode. You can also try using the fileread function instead of fread and since you are not providing the full path please add a check before fid to make sure that it is valid.
fileName = 'filename.json'; % filename in JSON extension
str = fileread(fileName); % dedicated for reading files as text
data = jsondecode(str); % Using the jsondecode function to parse JSON from string
The following link provides the information for decoding or creating JSON formatted text.