Kevin
Kevin

Reputation: 229

Read JSON objects with Matlab

I want to read from a json file with Matlab and store everything in "data" as objects. After import, I need to iterate through all and extract specific values, if it's available in the object.

JSON (source):

{
    "eid": 44000, 
    "dpm_id": {
        "dpm": "fm", 
        "pwr": "main"
    }, 
    "fpga_id": 3189637128, 
    "fpga_ver": 3104379702, 
    "boot_id": 0, 
    "pbs_ver": "PBS 2012-05-07 16:41"
}
{
    "sid": 1, 
    "hk1": {
        "bela_mode": "pbs_mode", 
        "pbs_version": "version 1.3", 
        "scet": "2038-01-19T03:14:08", 
        "ref_time": "0:00:00", 
        "tc_received": 2, 
        "tc_exec": 2, 
        "tc_err_ack": 0, 
        "tc_err_exec": 1, 
        "tm_total": 1, 
        "tm_sent": 1, 
        "tm_dropped": 0,
        ....

Matlab (import, according to this website, resp. Class):

fname = 'FileName.json';
fid = fopen(fname);
raw = fread(fid,inf);
str = char(raw');
fclose(fid);

data = JSON.parse(str)

Problem/Question:

As you see, Matlab only reads the content of the first brackets/field. How can I import ALL brackets/fields, even if I don't know how many there are?

data = 

         eid: 44000
      dpm_id: [1x1 struct]
     fpga_id: 3.1896e+09
    fpga_ver: 3.1044e+09
     boot_id: 0
     pbs_ver: 'PBS 2012-05-07 16:41'

Thank you!

Upvotes: 9

Views: 22211

Answers (4)

user664303
user664303

Reputation: 2063

If you just want to read whole JSON files into MATLAB, and have a C++11 compiler, you can use the very fast json_read mex function.

Upvotes: 0

Daniel
Daniel

Reputation: 36710

You are trying to read a json file, which is not valid. I recommend to use jsonlint for a quick verification.

Your json looks like

{
    "skipped":"A"    
}
{
    "skipped":"B"
}

That is not a valid syntax, because it describes two objects. After the first } the parser expects the end of file because a json file contains one object.

Possible fixes are:

[
    {
        "skipped": "A"
    },
    {
        "skipped": "B"
    }
]

or

{
    "aa": {
        "skipped": "A"
    },
    "bb": {
        "skipped": "B"
    }
}

Upvotes: 9

LowPolyCorgi
LowPolyCorgi

Reputation: 5189

If your file is accessible via http or https, you can use the webread function from the Data Import and Export toolbox. It automatically converts JSON files to Matlab structures.

There is a decodeJSON function in the toolbox (MATLABROOT/toolbox/matlab/external/interfaces/webservices/restful/private/decodeJSON.m), but the help clearly states that:

% FOR INTERNAL USE ONLY -- This function is intentionally undocumented
%   and is intended for use only within the scope of functions and classes
%   in toolbox/matlab/external/interfaces/webservices/restful. Its behavior
%   may change, or the class itself may be removed in a future release.

Nevertheless you can get inspiration in the content to build your own solution. It's a pity that the Mathworks didn't made this program available outside the toolbox.

Best

Upvotes: 4

Alan
Alan

Reputation: 3417

You could try parsing using another json library, such as this one on file exchange.

Alternatively you could try some of the methods listed on this site, such as using matlab's Java and .NET integration and loading with one of their json libraries.

As a third alternative, since the method you have shown above will happily load the first object in the string, you could always do some manual pre-parsing of the string into a cell array of strings containing a single object each, and then parse those.

Upvotes: 3

Related Questions