How can I read a .json file?

1.677 visualizaciones (últimos 30 días)
Carlyle Hickman
Carlyle Hickman el 24 de Feb. de 2017
Respondida: Hammad Ur Rahman el 9 de Feb. de 2024
Hello I found some data on the web that requires me to open a .gz file and a .json. I was able to unzip the gz file in MATLAB but have not been able to read the .json.
My Code:
>> fname = 'meta_Electronics.json';
>> fid = fopen(fname);
>> raw = fread(fid,inf);
>> str = char(raw');
>> fclose(fid);
>> data = JSON.parse(str)
and this is the error that is displayed:
Undefined variable "JSON" or class "JSON.parse".
Can someone point me in the right direction, as far as what i need to change?
Cheers.
  1 comentario
Adam
Adam el 24 de Feb. de 2017
I looked on the File Exchange when I wanted to read a json file, then downloaded something from there and made various edits to it to speed it up a bit. There are a few submissions on there that will read json files.
If you haven't defined anything called JSON though then
data = JSON.parse(str);
is highly optimistic coding. It's a very good way to code from the top level down using idealised code, but only if you write the code that you are calling too rather than just assume it will appear somehow!

Iniciar sesión para comentar.

Respuesta aceptada

Alka Nair
Alka Nair el 22 de Nov. de 2017
fname = 'meta_Electronics.json';
fid = fopen(fname);
raw = fread(fid,inf);
str = char(raw');
fclose(fid);
val = jsondecode(str);
worked for me.
  4 comentarios
Tolga Karabiyikoglu
Tolga Karabiyikoglu el 12 de Nov. de 2020
Cool- seems to work for me too
Juan Miguel Serrano Rodríguez
Juan Miguel Serrano Rodríguez el 19 de Oct. de 2021
Thanks!

Iniciar sesión para comentar.

Más respuestas (3)

Elbi Mutluoglu
Elbi Mutluoglu el 13 de Feb. de 2020
How can I read this json file as array.
{"flowSegmentData":{"frc":"FRC0","currentSpeed":30,"freeFlowSpeed":85,"currentTravelTime":28,"freeFlowTravelTime":10,"confidence":0.9900000095367432,"roadClosure":false,"coordinates":{"coordinate":[{"latitude":40.99520713231141,"longitude":29.11316083692566},{"latitude":40.9951128720138,"longitude":29.11348934584032},{"latitude":40.99499782625885,"longitude":29.113922597143414},{"latitude":40.99492447823734,"longitude":29.11425589372996},{"latitude":40.99481265425299,"longitude":29.114754874308034},{"latitude":40.9947156413892,"longitude":29.115239700057685},{"latitude":40.99469244741794,"longitude":29.115362205564793},{"latitude":40.99466308297875,"longitude":29.115527466260318},{"latitude":40.99465731538117,"longitude":29.11556668313179},{"latitude":40.99465324816397,"longitude":29.115588820406316},{"latitude":40.9946365659001,"longitude":29.115684510886638},{"latitude":40.994617484549394,"longitude":29.11581043877149},{"latitude":40.99460761712758,"longitude":29.115881261564937}]},"@version":"traffic-service 3.2.001"}}
  1 comentario
Nitesh Panchal
Nitesh Panchal el 26 de Abr. de 2021
Editada: Nitesh Panchal el 26 de Abr. de 2021
please use----- {jsondecode('[ ----------------please paste your json code here------------- ]')}
{jsondecode('[{"flowSegmentData":{"frc":"FRC0","currentSpeed":30,"freeFlowSpeed":85,"currentTravelTime":28,"freeFlowTravelTime":10,"confidence":0.9900000095367432,"roadClosure":false,"coordinates":{"coordinate":[{"latitude":40.99520713231141,"longitude":29.11316083692566},{"latitude":40.9951128720138,"longitude":29.11348934584032},{"latitude":40.99499782625885,"longitude":29.113922597143414},{"latitude":40.99492447823734,"longitude":29.11425589372996},{"latitude":40.99481265425299,"longitude":29.114754874308034},{"latitude":40.9947156413892,"longitude":29.115239700057685},{"latitude":40.99469244741794,"longitude":29.115362205564793},{"latitude":40.99466308297875,"longitude":29.115527466260318},{"latitude":40.99465731538117,"longitude":29.11556668313179},{"latitude":40.99465324816397,"longitude":29.115588820406316},{"latitude":40.9946365659001,"longitude":29.115684510886638},{"latitude":40.994617484549394,"longitude":29.11581043877149},{"latitude":40.99460761712758,"longitude":29.115881261564937}]},"@version":"traffic-service 3.2.001"}}]')}

Iniciar sesión para comentar.


cui,xingxing
cui,xingxing el 14 de Ag. de 2023
My personal opinion is that there is still no direct mapping from a generic readable json file to MATLAB's built-in types (struct or dictionary). The readstruct function is expected to support more file formats.
But on the other hand, you don't really need to call low-level functions such as fread, you can just use the fileread function to read the content and then decode it. For example, the following operation:
!unzip test.zip # see attachment file,use @Elbi Mutluoglu data for example
Archive: test.zip inflating: test.json
View json file contents, preview it.
!cat test.json
{ "flowSegmentData": { "frc": "FRC0", "currentSpeed": 30, "freeFlowSpeed": 85, "currentTravelTime": 28, "freeFlowTravelTime": 10, "confidence": 0.9900000095367432, "roadClosure": false, "coordinates": { "coordinate": [ { "latitude": 40.99520713231141, "longitude": 29.11316083692566 }, { "latitude": 40.9951128720138, "longitude": 29.11348934584032 }, { "latitude": 40.99499782625885, "longitude": 29.113922597143414 }, { "latitude": 40.99492447823734, "longitude": 29.11425589372996 }, { "latitude": 40.99481265425299, "longitude": 29.114754874308034 }, { "latitude": 40.9947156413892, "longitude": 29.115239700057685 }, { "latitude": 40.99469244741794, "longitude": 29.115362205564793 }, { "latitude": 40.99466308297875, "longitude": 29.115527466260318 }, { "latitude": 40.99465731538117, "longitude": 29.11556668313179 }, { "latitude": 40.99465324816397, "longitude": 29.115588820406316 }, { "latitude": 40.9946365659001, "longitude": 29.115684510886638 }, { "latitude": 40.994617484549394, "longitude": 29.11581043877149 }, { "latitude": 40.99460761712758, "longitude": 29.115881261564937 } ] }, "@version": "traffic-service 3.2.001" } }
myJsonFile = "test.json";
text = fileread(myJsonFile);
data = jsondecode(text)
data = struct with fields:
flowSegmentData: [1×1 struct]
% Access to the next level
fieldnames(data.flowSegmentData)
ans = 9×1 cell array
{'frc' } {'currentSpeed' } {'freeFlowSpeed' } {'currentTravelTime' } {'freeFlowTravelTime'} {'confidence' } {'roadClosure' } {'coordinates' } {'x_version' }
  1 comentario
cui,xingxing
cui,xingxing el 24 de Ag. de 2023
Luckily, my expectations above (readstruct support for json reads, writestruct support for json saves) have been implemented by the new version, which has been supported since R2023b!

Iniciar sesión para comentar.


Hammad Ur Rahman
Hammad Ur Rahman el 9 de Feb. de 2024
% Read JSON data from a file
jsonFileName = 'data.json';
jsonStr = fileread(jsonFileName);
% Convert JSON string to MATLAB variables
jsonData = jsondecode(jsonStr);
% Access JSON data using MATLAB variables
value = jsonData.key; % Replace 'key' with the actual key name

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by