Clear Filters
Clear Filters

converting an model.xml to model.json with cobra

21 views (last 30 days)
Hi,
I have build a model in Python in xml format and want to convert it to a json format so i can use it for visualization with Escher. I loaded the xml model in Matlab but I can only get it converted into a .mat file and get an error when I want to get json format model. I tried the following:
1. outmodel = writeCbModel(model, 'format','json', 'fileName', 'TestModel.json')
Error using writeCbModel (line 229)
Unknown file format
Error in MBA_testfile (line 35)
outmodel = writeCbModel(model, 'format','json', 'fileName', 'TestModel.json')
2. cobra.io.json.to_json(model)
Unable to resolve the name cobra.io.json.to_json.
Error in MBA_testfile (line 20)
cobra.io.json.to_json(model)

Answers (1)

Ishan Gupta
Ishan Gupta on 28 Jul 2022
MATLAB has a built in function called “jsonencode()” to convert a struct to a JSON string.
Also, when writing into a file using “fprintf()" function the file Identifier which you created as the variable “fid” should be passed as the first argument to the function.
The following code might help you :
targets = load('targets.mat').targets;
distractors = load('distractors.mat').distractors;
level_number='Level 3' %example of a level number
for y=1:12
JSONFILE_name= sprintf('%s_JSON%d.json',level_number,y);
fid=fopen(JSONFILE_name,'w')
s = struct("audioData", targets{y}, "DistractorData", distractors{y});
encodedJSON = jsonencode(s);
fprintf(fid, encodedJSON);
end
fclose('all');
Refer to the following Links:
On “jsonencode()” function:
On “fprintf()fucntion:

Community Treasure Hunt

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

Start Hunting!