Convert JSON to Table and Query Specific Value

I have the following JSON array:
{"views":[{"Name":"A","Conf":"High","View":"Negative"}, {"Name":"B","Conf":"Low","View":"Negative"}, {"Name":"C","Conf":"Low","View":"Negative"}]}
How can I convert it to Table? Also, how can I query a specific row of the table? (Example: Find value of "Conf" where Name is "C.)
So far I have:
jsonData = '{"views":[{"Name":"A","Conf":"High","View":"Negative"}, {"Name":"B","Conf":"Low","View":"Negative"}, {"Name":"C","Conf":"Low","View":"Negative"}]}'
structData = jsondecode(jsonData);

 Akzeptierte Antwort

Florian Bidaud
Florian Bidaud am 15 Aug. 2023
Bearbeitet: Florian Bidaud am 15 Aug. 2023
jsonData = '{"views":[{"Name":"A","Conf":"High","View":"Negative"}, {"Name":"B","Conf":"Low","View":"Negative"}, {"Name":"C","Conf":"Low","View":"Negative"}]}'
jsonData = '{"views":[{"Name":"A","Conf":"High","View":"Negative"}, {"Name":"B","Conf":"Low","View":"Negative"}, {"Name":"C","Conf":"Low","View":"Negative"}]}'
structData = jsondecode(jsonData)
structData = struct with fields:
views: [3×1 struct]
structDataTable = struct2table(structData.views)
structDataTable = 3×3 table
Name Conf View _____ ________ ____________ {'A'} {'High'} {'Negative'} {'B'} {'Low' } {'Negative'} {'C'} {'Low' } {'Negative'}
structDataTable.Name
ans = 3×1 cell array
{'A'} {'B'} {'C'}
structDataTable.Conf{[structDataTable.Name{:}]=='C'}
ans = 'Low'

8 Kommentare

Thanks Florian . Can you please also suggest how to query specific row in the table/struct? Example : Find value of "Conf" where Name is "C.
Florian Bidaud
Florian Bidaud am 15 Aug. 2023
Bearbeitet: Florian Bidaud am 15 Aug. 2023
structDataTable.Conf{[structDataTable.Name{:}]=='C'}
Thank you so much
Can I please check how to query data for a string value? It works with 'C' but throw an error when I try the following code:
structDataTable.Conf{[structDataTable.Name{:}]=='Cat'};
Error: Matrix dimensions must agree.
Florian Bidaud
Florian Bidaud am 16 Aug. 2023
Bearbeitet: Florian Bidaud am 16 Aug. 2023
Ah yes sorry, you need to use strcmp as follows :
structDataTable.Conf{strcmp([structDataTable.Name(:)],'Cat')};
Thanks, it works when there is a match. However, it throws an error when it cant find a match.
Error : Unable to perform assignment with 0 elements on the right-hand side.
How can I handle this?
Florian Bidaud
Florian Bidaud am 16 Aug. 2023
Bearbeitet: Florian Bidaud am 16 Aug. 2023
when you affect your value, first check if it's empty or not:
if ~isempty(structDataTable.Conf{strcmp([structDataTable.Name(:)],'Cat')})
value = structDataTable.Conf{strcmp([structDataTable.Name(:)],'Cat')};
else
value = nan;
end
sorry, I am getting "Unable to perform assignment with 0 elements on the right-hand side." on the following line.
if ~isempty(structDataTable.Conf{strcmp([structDataTable.Name(:)],'Cat')})

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by