save json file to directory from workspace

18 views (last 30 days)
I've read a json file into a structure, edited the structure, re-encoded the structure as another json file in the workspace, then attempted to save it in the directory using the save function. It appears to have an issue here as instead of saving correctly it seems to be encoding wrong as shown below. first image is the view when i open the original json file which is correctly displayed. second image is the view when i open the new json file from the directory. it is stored correctly in the workspace so the issue is amost certainly from the save function - what is the correct method for saving the json file?
Thanks,

Accepted Answer

Jan
Jan on 24 Mar 2018
Edited: Jan on 24 Mar 2018
No, this is not a "wrong" encoding, but save stores the data in MAT file format. sav is simply the wrong function to store a JSON file.
What did you use to import the JSON file?
I'd start with searching in the net. I'd prefer a stable Java implementation like http://vision.is.tohoku.ac.jp/~kyamagu/software/json/. You find many other implementations in the FileExchange: https://www.mathworks.com/matlabcentral/fileexchange/?utf8=%E2%9C%93&term=json . JSONlab is frequently downloaded, but I'm not a fan of it because of the usage of global variables for the communication with the subfunctions. What a pity that such a huge and useful tool suffers from a design flaw.
Of course Matlab's jsonencode should be fine.
  4 Comments
George Swindells
George Swindells on 24 Mar 2018
Yeah I understood that thanks, and thanks a lot for the example code there, Yeah I don’t normally use “ans” I was just using it to test the output, i will define something properly next.
Thanks again,
Doaa Alamoudi
Doaa Alamoudi on 27 Apr 2021
To import json file to matlab
DF = 'filename.json';
fid = fopen(DF);
raw = fread(fid,inf);
str = char(raw');
data = jsondecode(str);

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!