Extract info from .json file by matlab

488 views (last 30 days)
Zeynab Mousavikhamene
Zeynab Mousavikhamene on 5 Aug 2019
Commented: Walter Roberson 32 minuten ago
I need to parse a json file and extract many objects and arrays from that file. Then I am going to plot those extracted information.
I used jsondecode but it only created one big char. Shoud I use that?

Answers (3)

Neeraj Rajpurohit
Neeraj Rajpurohit on 30 Jun 2020
fileName = 'filename.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
  3 Comments
Vairamani Kanagavel
Vairamani Kanagavel on 12 Oct 2021
tried and working fine for reading .json files

Sign in to comment.


Navya Seelam
Navya Seelam on 8 Aug 2019
Hi,
In order to plot the data use str2num to covert the string to numeric data type. If the data in JSON was array of numbers rather than strings then the output of jsondecode would have been array of double.
For more details on conversion of JSON datatypes to MATLAB data types refer this link

Arvind Gauns
Arvind Gauns ongeveer 4 uur ago
I have a bunch of JSON files to get infornmation from. Eah file has got three structs within it.
I want to run a batch process to read all the 50 files and seperate the three structs and compile them into three csv files.
Can you help me with it ? I am sharing my code herebelow. Thank you in advance.
[file_list, path_n] = uigetfile('*dark.pico', 'Grab all the files', 'MultiSelect','on');
if iscell (file_list)== 0
file_list = (file_list)
end
T = array2table(file_list);
for i = 1:length(file_list);
loadfile = loadjson('{1}');
s = struct(loadfile);
x = s.Spectra(1).Pixels(:);
T1 = table(x);
x1 = s.Spectra(1).Metadata().Datetime;
x2 = s.Spectra(1).Metadata().Direction;
writetable(T1,'filename1.csv')
y = s.Spectra(2).Pixels(:);
T2 = table(y);
y1 = s.Spectra(2).Metadata().Datetime;
y2 = s.Spectra(2).Metadata().Direction;
writetable(T2,'filename2.csv')
z = s.Spectra(3).Pixels(:);
T3 = table(z);
z1 = s.Spectra(3).Metadata().Datetime;
z2 = s.Spectra(3).Metadata().Direction;
writetable(T3,'filename3.csv')
end
  1 Comment
Walter Roberson
Walter Roberson 32 minuten ago
[file_list, path_n] = uigetfile('*dark.pico', 'Grab all the files', 'MultiSelect','on');
if ischar(file_list); file_list = {file_list}; end %only one selected
if ~iscell(file_list); return; end %user cancel
T = array2table(file_list);
for i = 1:length(file_list);
this_file = fullfile(path_n, filelist{i});
loadfile = loadjson(this_file);
s = struct(loadfile);
x = s.Spectra(1).Pixels(:);
T1 = table(x);
x1 = s.Spectra(1).Metadata().Datetime;
x2 = s.Spectra(1).Metadata().Direction;
writetable(T1,'filename1.csv')
y = s.Spectra(2).Pixels(:);
T2 = table(y);
y1 = s.Spectra(2).Metadata().Datetime;
y2 = s.Spectra(2).Metadata().Direction;
writetable(T2,'filename2.csv')
z = s.Spectra(3).Pixels(:);
T3 = table(z);
z1 = s.Spectra(3).Metadata().Datetime;
z2 = s.Spectra(3).Metadata().Direction;
writetable(T3,'filename3.csv')
end
However:
You do not use x1, x2, y1, y2, z1, z2 after you create them.
You are always overwriting all of the csv file content.
Instead of using tables for that purpose, you should consider using writematrix().

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!