JSON (.json)

Background & Context

    • MIME type: application/json.
    • Data interchange format.
    • Supports full JSON syntax but uses a specific interpretation.
    • Identifies JSON objects with lists of rules in the Wolfram Language.
    • Commonly used in web programming.
    • JSON is an acronym derived from JavaScript Object Notation.
    • Based on a subset of the JavaScript programming language.
    • Plain text format.
    • Developed in 2001.
    • Published as RFC 4627.

Import & Export

  • Import["file.json"] gives a rule-based representation of a JSON file.
  • Export["file.json", expr] exports a list of rules to JSON.
  • Import["file.json","elem"] imports the specified element from a JSON file.
  • Import["file.json",{"elem","sub",}] imports a subelement.
  • Import["file.json",{{"elem1","elem2",}}] imports multiple elements.
  • The import format can be specified with Import["file","JSON"] or Import["file",{"JSON",elem,}].
  • Export["file.json",expr,"elem"] creates a JSON file by treating expr as specifying element elem.
  • Export["file.json",expr,opt1->val1,] exports expr with the specified option elements taken to have the specified values.
  • Export["file.json",{"elem1"->expr1,"elem2"->expr2,},"Rules"] uses rules to specify the elements to be exported.
  • See the following reference pages for full general information:
  • Import, Exportimport from or export to a file
    CloudImport, CloudExportimport from or export to a cloud object
    ImportString, ExportStringimport from or export to a string
    ImportByteArray, ExportByteArrayimport from or export to a byte array

Import Elements

  • General Import elements:
  • "Elements" list of elements and options available in this file
    "Summary"summary of the file
    "Rules"list of rules for all available elements
  • Data representation element:
  • "Data"contents of a JSON file as nested rules
    "Dataset"contents of a JSON file as a Dataset
  • Import by default uses the "Data" element.
  • The "JSON" format identifies JSON objects of the form {field1:value1,field2:value2,} with lists of rules of the form {"field1"->value1,"field2"->value2,} in the Wolfram Language.
  • "JSON" identifies JSON lists [e1,e2,] with Wolfram Language lists {e1,e2,}.
  • Strings in the Wolfram Language are represented in JSON as UTF-8 strings, escaped as required by the JSON standard.
  • Numbers where scientific notation is used are given in JSON in E notation.
  • The symbols True and False are represented in JSON as the values true and false.
  • The symbol Null is represented as the JSON value null.

Options

  • Export option:
  • "Compact"Falsewhether to use line breaks to format file content

Examples

open allclose all

Basic Examples  (2)

Import the contents of a JSON file:

Export a list of chemical elements and their properties to a JSON file:

Scope  (1)

Create a compact string representation of a JSON file:

Import Elements  (2)

"Data"  (1)

Import a JSON file as nested lists:

"Dataset"  (1)

Import a JSON file as a Dataset:

Possible Issues  (1)

ImportString cannot import as JSON from strings containing non-ASCII characters:

Convert the data to UTF-8 as required by the JSON standard to import such strings: