Filter löschen
Filter löschen

How can I read a .json file?

1.483 Ansichten (letzte 30 Tage)
Carlyle Hickman
Carlyle Hickman am 24 Feb. 2017
Bearbeitet: cui,xingxing am 27 Apr. 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 Kommentar
Adam
Adam am 24 Feb. 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!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Alka Nair
Alka Nair am 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 Kommentare
Tolga Karabiyikoglu
Tolga Karabiyikoglu am 12 Nov. 2020
Cool- seems to work for me too
Juan Miguel Serrano Rodríguez
Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Elbi Mutluoglu
Elbi Mutluoglu am 13 Feb. 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 Kommentar
Nitesh Panchal
Nitesh Panchal am 26 Apr. 2021
Bearbeitet: Nitesh Panchal am 26 Apr. 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"}}]')}

Melden Sie sich an, um zu kommentieren.


cui,xingxing
cui,xingxing am 14 Aug. 2023
Bearbeitet: cui,xingxing am 27 Apr. 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 Kommentar
cui,xingxing
cui,xingxing am 24 Aug. 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!

Melden Sie sich an, um zu kommentieren.


Hammad Ur Rahman
Hammad Ur Rahman am 9 Feb. 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