Main Content

writedictionary

Write dictionary to file

Since R2024b

    Description

    writedictionary(d,filename) writes the contents of dictionary d to a file with the name and extension specified by filename. For example, the writedictionary function writes the input dictionary to a JSON file when .json is specified as the file extension in filename.

    example

    writedictionary(d,filename,Name=Value) specifies options using one or more name-value arguments. For example, you can export the contents of the input dictionary as a JSON file regardless of the file extension specified in filename by calling writedictionary(d,filename,FileType="json").

    example

    Examples

    collapse all

    Create a dictionary and write it to a JSON file.

    wheels = [1 2 3];
    names = ["Unicycle" "Bicycle" "Tricycle"];
    d = dictionary(names,wheels);
    writedictionary(d,"vehicles.json");

    Display the created file.

    type vehicles.json
    {
        "Unicycle": 1.0,
        "Bicycle": 2.0,
        "Tricycle": 3.0
    }
    

    Create a dictionary and write it to a JSON file without indentation.

    wheels = [1 2 3];
    names = ["Unicycle" "Bicycle" "Tricycle"];
    d = dictionary(names,wheels);
    writedictionary(d,"vehicles.json",PrettyPrint=false);

    Display the created file.

    type vehicles.json
    {"Unicycle":1.0,"Bicycle":2.0,"Tricycle":3.0}
    

    Input Arguments

    collapse all

    Input dictionary, specified as a MATLAB dictionary. A dictionary is a map that stores data as values, which can be accessed using corresponding unique keys. Each pair of keys and values is an entry. For more information on dictionaries, see dictionary.

    When creating JSON files, writedictionary writes missing values as null values and NaN values as null or NaN values depending on the PreserveInfAndNaN name-value argument.

    Name of the file to write, specified as a string scalar or character vector. If filename does not exist, then the writing function creates the file. If filename is an existing file, then the function overwrites it.

    Depending on the location you are writing to, filename can take one of these forms.

    Location

    Form

    Current folder

    To write to the current folder, specify the name of the file in filename.

    Example: "myFile.json"

    Other folders

    To write to a folder that is not the current folder, specify the full or relative pathname in filename.

    Example: "C:\myFolder\myFile.json"

    Example: "myFolder\myFile.json"

    Remote location

    To write to a remote location, specify a uniform resource locator (URL) of the form:

    scheme_name://path_to_file/my_file.ext

    Based on the remote location, scheme_name can be one of the values in this table.

    Remote Locationscheme_name
    Amazon S3™s3
    Windows Azure® Blob Storagewasb, wasbs
    HDFS™hdfs

    For more information, see Work with Remote Data.

    Example: "s3://bucketname/path_to_file/my_file.json"

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: writedictionary(d,filename,PreserveInfandNaN=false) writes all Inf and NaN values as JSON null values.

    Type of file, specified as one of these values:

    • "auto" — Automatically detect the file format to write from the extension specified in filename.

    • "json" — Export the contents of the dictionary as a JSON file, regardless of the file extension specified in filename.

    If you specify a file extension in filename that is not .json, you can specify FileType as "json" to write the contents of the input dictionary as JSON.

    Indent text in the output file, specified as a numeric or logical 1 (true) or 0 (false). If you specify the argument as true, then writedictionary writes the JSON text with an indentation of four spaces. If you specify the argument as false, then writedictionary writes the JSON text without spaces or line breaks.

    Preserve Inf and NaN values in the output JSON file, specified as a numeric or logical 1 (true) or 0 (false). Specify this argument as false to write all Inf and NaN values as JSON null values.

    Version History

    Introduced in R2024b