Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

jsonencode

Create JSON-formatted text from structured MATLAB data

Syntax

text = jsonencode(data)
text = jsonencode(data,'ConvertInfAndNaN',TF)

Description

example

text = jsonencode(data) encodes data and returns a character vector in JSON format.

text = jsonencode(data,'ConvertInfAndNaN',TF) customizes the encoding of special floating point values NaN, Inf, -Inf.

Examples

collapse all

value = {'one'; 'two'; 'three'};
jsonencode(value)
ans = 
'["one","two","three"]'

Input Arguments

collapse all

MATLAB data, specified as any MATLAB data type except enumerations.

Example: s.IDs = [116, 943, 234, 38793]

Customize the encoding of special floating point values NaN, Inf, and -Inf, specified as true or false. A true value encodes floating point values as null. A false value encodes the values as literal NaN, Infinity, or -Infinity.

Example: jsonencode(-Inf,'ConvertInfAndNaN',false)

Output Arguments

collapse all

JSON-formatted text, returned as a character vector.

Limitations

  • jsonencode does not support complex numbers, sparse arrays, or MATLAB enumerations.

  • jsonencode does not support recursive structures such as graphics objects that contain references to parent and child objects.

  • If you encode, then decode a value, MATLAB does not guarantee that the data type is preserved. JSON supports fewer data types than MATLAB, which results in loss of type information. For example, JSON data does not distinguish between double and int32. If you encode an int32 value and then call jsondecode, the decoded value is type double.

  • MATLAB does not guarantee that the shape of an array is preserved. For example, a 1-by-N numeric vector is encoded as an array. If you call jsondecode, then MATLAB decodes the array as an N-by-1 vector.

Algorithms

JSON supports fewer data types than MATLAB. jsonencode converts MATLAB data types to the JSON data types listed here.

MATLAB Data Type

JSON Data Type

Example

Output

array, empty

Array, empty

jsonencode([])
jsonencode(string.empty)
'[]'

logical scalar

Boolean

jsonencode(true)
'true'

logical vector

Array of booleans

jsonencode([true,false,false])
'[true,false,false]'

logical array

Nested array of booleans

jsonencode(logical([0,1,0,1,1,0]))
'[false,true,false,true,true,false]'

character vector

String

jsonencode('This is a char.')
'"This is a char."'

character array

Array of strings

jsonencode(['AC';'EG'])
'["AC","EG"]'

string scalar

String

jsonencode("This is a string.")
'"This is a string."'

string vector

Array of strings

jsonencode(["AC";"EG"])
'["AC","EG"]'

string array

Nested array of strings

jsonencode(["AC","EG";"BD","FH"]])
'[["AC","EG"],["BD","FH"]]'

empty character vector

String

jsonencode('')
'""'

<missing>

null

jsonencode(string(nan))
'null'

numeric scalar

Number

jsonencode(2.5)
'2.5'

numeric vector

Array of numbers

jsonencode(1:3)
'[1,2,3]'

numeric array

Nested array of numbers

jsonencode(eye(2))
'[[1,0],[0,1]]'

complex numbers

Not supported

  

table

Array of objects

Name = {'Jones';'Brown'};
Age = [40;49];
jsonencode(table(Name,Age))
'[{"Name":"Jones","Age":40},{"Name":"Brown","Age":49}]'

cell

Array

jsonencode({'a',true,[2;3]})
'["a",true,[2,3]]'

structure scalar
object scalar

Object
Object (Public properties encoded as name-value pairs.)

jsonencode(struct('a','value'))
'{"a":"value"}'

structure vector
object vector

Array of objects

jsonencode(struct('a',{true,true,false}))
'[{"a":true},{"a":true},{"a":false}]'

structure array
object array

Nested array of objects

  

datetime scalar

String (string method used to convert date and time to string format.)

jsonencode(datetime('tomorrow'))
'"04-Nov-2016"'

datetime vector

Array of strings

  

datetime array

Nested array of strings

DT = datetime({'8 April 2015','9 May 2015'}, ...
    'InputFormat','d MMMM yyyy');
jsonencode(DT)
'["08-Apr-2015","09-May-2015"]'

categorical scalar

String (string method used to create string format.)

jsonencode(categorical({'r';'g';'b'}))
'["r","g","b"]'

categorical vector

Array of strings

jsonencode(categorical( ...
    {'r' 'b' 'g'; ...
    'g' 'r' 'b'; ...
    'b' 'r' 'g'}))
'[["r","b","g"],["g","r","b"],["b","r","g"]]'

categorical array

Nested array of strings

  

containers.Map

Object

jsonencode(containers.Map( ...
    {'Jan','Feb','Mar'}, ...
    [327,368,197]))
'{"Feb":368,"Jan":327,"Mar":197}'

NaN
Inf

null

jsonencode([1,2,NaN,3,Inf])
'[1,2,null,3,null]'

Introduced in R2016b

Was this topic helpful?