{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
>> data=loadjson('example1.json')
data =
firstName: 'John'
lastName: 'Smith'
age: 25
address: [1x1 struct]
phoneNumber: [1x2 struct]
>> savejson('',data)
ans =
{
"data": {
"firstName": "John",
"lastName": "Smith",
"age": [25],
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
}
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
>> data=loadjson('example2.json')
data =
{
glossary =
{
title = example glossary
GlossDiv =
{
1x1 struct array containing the fields:
title: 1x1 sq_string
GlossList: 1x1 struct
}
}
}
>> savejson('',data)
ans = {
"data": {
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": "GMLXML"
},
"GlossSee": "markup"
}
}
}
}
}
}
{"menu": {
"id": "file",
"value": "_&File",
"popup": {
"menuitem": [
{"value": "_&New", "onclick": "CreateNewDoc(\"\")"},
{"value": "_&Open", "onclick": "OpenDoc()"},
{"value": "_&Close", "onclick": "CloseDoc()"}
]
}
}}
>> data=loadjson('example3.json')
data =
{
menu =
{
id = file
value = _&File
popup =
{
1x1 struct array containing the fields:
menuitem: 0x6 string
}
}
}
>> savejson('',data)
ans = {
"data": {
"menu": {
"id": "file",
"value": "_&File",
"popup": {
} } } }
Define a MATLAB structure, notice the handling to the special variables "NaN" and "Inf":
a=struct('node',[1 9 10; 2 1 1.2], 'elem',[9 1;1 2;2 3],...
'face',[9 01 2; 1 2 3; NaN,Inf,-Inf], 'author','FangQ');
octave:8> a
a =
{
node =
1.0000 9.0000 10.0000
2.0000 1.0000 1.2000
elem =
9 1
1 2
2 3
face =
9 1 2
1 2 3
NaN Inf -Inf
author = FangQ
}
octave:10> savejson('obj',a)
ans = {
"obj": {
"node": [
[1,9,10],
[2,1,1.2]
],
"elem": [
[9,1],
[1,2],
[2,3]
],
"face": [
[9,1,2],
[1,2,3],
["_NaN_","_Inf_","-_Inf_"]
],
"author": "FangQ"
}
}
octave:11> data=loadjson(savejson('obj',a))
data =
{
obj =
{
node =
1.0000 9.0000 10.0000
2.0000 1.0000 1.2000
elem =
9 1
1 2
2 3
face =
9 1 2
1 2 3
NaN Inf -Inf
author = FangQ
}
}
Using the same "a" structure, we force savejson to use specialized structure to save array
octave:14> savejson('',a,struct('ArrayToStruct',1))
ans = {
"a": {
"node": {
"_ArrayType_": "double",
"_ArraySize_": [2,3],
"_ArrayData_": [1,2,9,1,10,1.2]
},
"elem": {
"_ArrayType_": "double",
"_ArraySize_": [3,2],
"_ArrayData_": [9,1,2,1,2,3]
},
"face": {
"_ArrayType_": "double",
"_ArraySize_": [3,3],
"_ArrayData_": [9,1,"_NaN_",1,2,"_Inf_",2,3,"-_Inf_"]
},
"author": "FangQ"
}
}
octave:15> data=loadjson(ans)
data =
{
a =
{
node =
1.0000 9.0000 10.0000
2.0000 1.0000 1.2000
elem =
9 1
1 2
2 3
face =
9 1 2
1 2 3
NaN Inf -Inf
author = FangQ
}
}
For sparse arrays, savejson uses _ArrayIsSparse_ keyword.
a=sparse(100,100);
a(11,9)=1;
a(30,60)=10;
savejson('',a)
ans = {
"_ArrayType_": "double",
"_ArraySize_": [100,100],
"_ArrayIsSparse_": 1,
"_ArrayData_": [
[11,9,1],
[30,60,10]
]
}
Converting back producing the same sparse array:
octave:20> data=loadjson(ans)
data = Compressed Column Sparse (rows = 100, cols = 100, nnz = 2 [0.02%]) (11, 9) -> 1 (30, 60) -> 10
In JSON, a "name" tag can be any valid Unicode strings (with escape sequences). However, MATLAB field/variable names can only use [a-zA-Z0-9_]. To convert an arbitrary JSON string, we have to convert these "invalid" characters into a valid MATLAB variable names. Here is an example
octave:21> data=loadjson('{"ValidName":1, "_InvalidName":2, ":Field:":3,"项目":'test'}')
data =
{
ValidName = 1
x_InvalidName = 2
x_0x3A_Field_0x3A_ = 3
x_0xE9__0xA1__0xB9__0xE7__0x9B__0xAE_ = test
}
Notice any tags begins with an "_" is converted to "x_...", similar to the convention used by genvarname() function in MATLAB. For any other invalid characters, it will be converted to "_0xXX_" where XX is the "%X" formatted string.