It seems that it is possible to handle JSON format from MATLAB 2016b.
jsondecode
jsonencode
Prepare the following files and check how to use them.
test.json
[
{
"Null": null,
"Boolean": true,
"Numeric": 1,
"String": "string",
"Object": { "a": null, "b": true, "c": 1, "d": "string" },
"Array": [ null, true, 1, "string" ],
"BooleanArray": [ true, false],
"NumericArray": [ 1, 2, 3 ],
"StringArray": [ "foo", "buz" ],
"ObjectArrayS": [ { "a": 1, "b": "one" }, { "a": 2, "b": "two" } ],
"ObjectArrayC": [ { "a": 1, "b": "one" }, { "x": 9, "y": true } ]
}
]
: one: Read JSON file
Use ** jsondecode **
>> json = jsondecode(fileread('test.json'))
json =
Struct with field:
Null: []
Boolean: 1
Numeric: 1
String: 'string'
Object: [1×1 struct]
Array: {4×1 cell}
BooleanArray: [2×1 logical]
NumericArray: [3×1 double]
StringArray: {2×1 cell}
ObjectArrayS: [2×1 struct]
ObjectArrayC: {2×1 cell}
: two: Encode structured MATLAB data as JSON-formatted text
Use ** jsonencode **.
The output of this function is a string representing a one-line JSON object.
It should be noted here that jsonencode cannot output null.
| JSON | MATLAB | MATLAB | JSON | |||
|---|---|---|---|---|---|---|
| null | :arrow_right: | [] | [] | :arrow_right: | [] |
>> jsonencode(json)
ans =
'{"Null":[],"Boolean":true,"Numeric":1,"String":"string","Object":{"a":[],"b":true,"c":1,"d":"string"},"Array":[[],true,1,"string"],"BooleanArray":[true,false],"NumericArray":[1,2,3],"StringArray":["foo","buz"],"ObjectArrayS":[{"a":1,"b":"one"},{"a":2,"b":"two"}],"ObjectArrayC":[{"a":1,"b":"one"},{"x":9,"y":true}]}'
Use low-level file I / O to write text files.
>> fileID = fopen('test2.json', 'w');
>> fprintf(fileID, jsonencode(json));
>> fclose(fileID);
Unfortunately, the current version of matlab doesn't support JSON formatting.
Therefore, use python's json.tool to format the JSON file output by matlab.
It's a bit sloppy, but if you have an environment where you can run python on the command line, you can run python on the matlab console as well.
https://jp.mathworks.com/help/matlab/matlab_external/run-external-commands-scripts-and-programs.html
>> !python -m json.tool test2.json test3.json
test3.json
{
"Null": [],
"Boolean": true,
"Numeric": 1,
"String": "string",
"Object": {
"a": [],
"b": true,
"c": 1,
"d": "string"
},
"Array": [
[],
true,
1,
"string"
],
"BooleanArray": [
true,
false
],
"NumericArray": [
1,
2,
3
],
"StringArray": [
"foo",
"buz"
],
"ObjectArrayS": [
{
"a": 1,
"b": "one"
},
{
"a": 2,
"b": "two"
}
],
"ObjectArrayC": [
{
"a": 1,
"b": "one"
},
{
"x": 9,
"y": true
}
]
}
The position of the line break is slightly different, but it can be reproduced except for ** null **.
The difficulty is that an intermediate file (test2.json) is generated.
The data that can be represented by MATLAB and JSON are not completely compatible. Refer to the following when expressing data so as to maintain compatibility with MATLAB variables. https://jp.mathworks.com/help/mps/restfuljson/json-representation-of-matlab-data-types.html However, reading for this format does not seem to be implemented.
Recommended Posts