Effacer les filtres
Effacer les filtres

How can I read a .json file?

1 483 vues (au cours des 30 derniers jours)
Carlyle Hickman
Carlyle Hickman le 24 Fév 2017
Modifié(e) : cui,xingxing le 27 Avr 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 commentaire
Adam
Adam le 24 Fév 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!

Connectez-vous pour commenter.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (3)

Elbi Mutluoglu
Elbi Mutluoglu le 13 Fév 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 commentaire
Nitesh Panchal
Nitesh Panchal le 26 Avr 2021
Modifié(e) : Nitesh Panchal le 26 Avr 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"}}]')}

Connectez-vous pour commenter.


cui,xingxing
cui,xingxing le 14 Août 2023
Modifié(e) : cui,xingxing le 27 Avr 2024
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' }
-------------------------Off-topic interlude, 2024-------------------------------
I am currently looking for a job in the field of CV algorithm development, based in Shenzhen, Guangdong, China,or a remote support position. I would be very grateful if anyone is willing to offer me a job or make a recommendation. My preliminary resume can be found at: https://cuixing158.github.io/about/ . Thank you!
Email: cuixingxing150@gmail.com
  1 commentaire
cui,xingxing
cui,xingxing le 24 Août 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!

Connectez-vous pour commenter.


Hammad Ur Rahman
Hammad Ur Rahman le 9 Fév 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

Community Treasure Hunt

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

Start Hunting!

Translated by