- Read JSON file as a string using fileread
- Decode the JSON structure using jsondecode
- Access the required fields using structure indexing.
How to open multiple .json files and parse the data in it in automated way?
15 次查看(过去 30 天)
显示 更早的评论
In each .json file, there are mixtue of string and numbers. All .json files have the same struncture but numbers/strings changes in each file.
I was wondering if there is matlab fucntion that I can extract info from each json file similar to the excel file. In excel file we can call a specific column, a specific row and a specifi cell playing wtih string and numbers (e.g. A1, B2, or so). I need to do something similar in .json but do not know how to parse through the file.
0 个评论
回答(1 个)
Aniket
2025-3-5
MATLAB provides jsondecode function to parse JSON into MATLAB structures and then access specific elements similar to how you reference Excel cells. Kindly follow below mentioned steps to parse JSON in MATLAB:
Example:
Consider you have a JSON file (data.json):
{
"A": {"a": 100, "b": 200},
"B": {"a": "Hello", "b": "World"}
}
This file can be read and extracted like shown below:
jsonStr = fileread('data.json');
data = jsondecode(jsonStr);
% Access specific data (like Excel cells A1, B2, etc.)
column = 'A';
row = 'a';
value = data.(column).(row); % Access "A.a" (100)
disp(value); % Output: 100
Please ensure that the fieldnames in JSON structure are valid MATLAB identifiers as mentioned in this documentation:
I hope this helps achieve the functionality you desired.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 JSON Format 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!