- 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?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Aniket
le 5 Mar 2025
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 commentaires
Voir également
Catégories
En savoir plus sur JSON Format dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!