RawJSON (.json)
Background & Context
-
- MIME type: application/json.
- Data interchange format.
- RawJSON supports full JSON syntax but uses a specific interpretation.
- RawJSON identifies JSON objects with Wolfram Language associations.
- JSON is commonly used in web programming.
- JSON is an acronym derived from JavaScript Object Notation.
- JSON is based on a subset of the JavaScript programming language.
- Plain text format.
- JSON was developed in 2001.
- JSON is published as RFC 4627.
Import & Export
- Import["file","RawJSON"] imports a JSON file as a combination of nested lists and associations.
- ImportString["string","RawJSON"] imports a JSON string.
- Export["file",expr,"RawJSON"] exports a combination of nested lists and associations to a JSON file.
- ExportString[expr,"RawJSON"] exports to a JSON string.
- See the following reference pages for full general information:
-
Import, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import 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 lists or associations "Dataset" contents of a JSON file as a Dataset - Import by default uses the "Data" element.
- The "RawJSON" format identifies JSON objects of the form {field1:value1,field2:value2,…} with associations of the form <|"field1"->value1,"field2"->value2,…|> in the Wolfram Language.
- "RawJSON" identifies JSON lists [e1,e2,…] with Wolfram Language lists {e1,e2,…}.
- Strings in the Wolfram Language are represented in RawJSON as UTF-8 strings, escaped as required by the JSON standard.
- Numbers where scientific notation is used are given in RawJSON in E notation.
- The symbols True and False are represented in RawJSON as the values true and false.
- The symbol Null is represented as the JSON value null.
Options
- Export options:
-
"Compact" False whether to omit line breaks and tabs "ConversionFunction" None function to apply for additional conversions "ConversionRules" {} rules to apply to override or add conversions - The setting for "ConversionRules" is used to replace subexpressions before built-in conversions are performed.
- The setting for "ConversionFunction" is applied to subexpressions for which no built-in conversion is defined.
- "Compact"->n includes line breaks and tabs up to indent level n.
Examples
open all close allBasic Examples (8)
ImportString["[1,2,3]", "JSON"]Give a string consisting of a JSON object:
"{\"x\":1, \"y\":2, \"z\":3}"Importing this string as an association:
ImportString[%, "RawJSON"]Import["http://en.wikipedia.org/w/api.php?action=query&prop=links&titles=Wolfram%20Language&continue=&format=json", "RawJSON"]Export an expression to a JSON string:
ExportString[{1, 2, 3}, "RawJSON", "Compact" -> True]ExportString[<|"x" -> 1, "y" -> 2, "z" -> 3|>, "RawJSON", "Compact" -> True]Export a list of chemical elements and their properties to a JSON file:
Export["elements.json", <|"Hydrogen" -> <|"AtomicNumber" -> 1, "AtomicWeight" -> 1.00793|>, "Helium" -> <|"AtomicNumber" -> 4.00259, "AtomicWeight" -> 2|>, "Lithium" -> <|"AtomicNumber" -> 3, "AtomicWeight" -> 6.94141|>, "Beryllium" -> <|"AtomicNumber" -> 4, "AtomicWeight" -> 9.01218|>, "Boron" -> <|"AtomicNumber" -> 5, "AtomicWeight" -> 10.8086|>, "Carbon" -> <|"AtomicNumber" -> "6", "AtomicWeight" -> 12.0107|>|>, "RawJSON"]ExportString[<|"Hydrogen" -> <|"AtomicNumber" -> 1, "AtomicWeight" -> 1.00793|>, "Helium" -> <|"AtomicNumber" -> 4.00259, "AtomicWeight" -> 2|>, "Lithium" -> <|"AtomicNumber" -> 3, "AtomicWeight" -> 6.94141|>, "Beryllium" -> <|"AtomicNumber" -> 4, "AtomicWeight" -> 9.01218|>, "Boron" -> <|"AtomicNumber" -> 5, "AtomicWeight" -> 10.8086|>, "Carbon" -> <|"AtomicNumber" -> "6", "AtomicWeight" -> 12.0107|>|>, "RawJSON"]Use conversion function to format dates to ISO 8601:
ExportString[
{<|"day" -> Yesterday, "T" -> "24.4"|>,
<|"day" -> Now, "T" -> "23.1"|>},
"RawJSON" , "ConversionFunction" -> (DateString[#, {"ISODateTime", "ISOTimeZone"}]&)]Use conversion rules to transform rules and quantities:
ExportString[<|"files" -> {"file1" -> Quantity["1.2", "Kilobytes"], "file2" -> Quantity["5.7", "Megabytes"]}|>, "RawJSON", "ConversionRules" -> {
Rule[file_, size_] :> <|"file" -> file, "size" -> size|>, Quantity[magnitude_, unit_] :> <|"magnitude" -> magnitude, "unit" -> unit|>}]Control the output format with "Compact", to limit indentation:
ExportString[<|"x" -> {{1, 2}, {3, 4}}, "y" -> {{Null}}|>, "RawJSON", "Compact" -> 1]Scope (1)
Create a compact string representation of a JSON file:
ExportString[{"Hydrogen" -> {"AtomicNumber" -> 1, "AtomicWeight" -> 1.00793}, "Helium" -> {"AtomicNumber" -> 4.00259, "AtomicWeight" -> 2}, "Lithium" -> {"AtomicNumber" -> 3, "AtomicWeight" -> 6.94141}, "Beryllium" -> {"AtomicNumber" -> 4, "AtomicWeight" -> 9.01218}, "Boron" -> {"AtomicNumber" -> 5, "AtomicWeight" -> 10.8086}, "Carbon" -> {"AtomicNumber" -> "6", "AtomicWeight" -> 12.0107}}, "JSON", "Compact" -> True]ImportString[%, "RawJSON"]Import Elements (2)
"Data" (1)
"Dataset" (1)
Import a JSON file as a Dataset:
Import["http://en.wikipedia.org/w/api.php?action=query&prop=links&titles=Wolfram%20Language&continue=&format=json", {"RawJSON", "Dataset"}]See Also
Related Guides
History
Introduced in 2015 (10.2) | Updated in 2022 (13.2)