OpenersPrimaryExamplesSection(Spacer1)Examples  (9)

OpenersExampleSection0(Spacer1)Basic Examples  (9)

Import a JSON list:

ImportString["[1,2,3]", "RawJSON"]

Give a string consisting of a JSON object:

"{\"x\":1, \"y\":2, \"z\":3}"

Importing this string gives an association:

ImportString[%, "RawJSON"]

Import the contents of a JSON file:

Import["ExampleData/temperatures.json", "RawJSON"]

Import a URL:

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[\[LeftAssociation]"x"->1,"y"->2,"z"->3\[RightAssociation], "RawJSON","Compact"->True]

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

Export["elements.json",\[LeftAssociation]"Hydrogen"->\[LeftAssociation]"AtomicNumber"->1,"AtomicWeight"->1.00793`\[RightAssociation],"Helium"->\[LeftAssociation]"AtomicNumber"->4.00259`,"AtomicWeight"->2\[RightAssociation],"Lithium"->\[LeftAssociation]"AtomicNumber"->3,"AtomicWeight"->6.94141`\[RightAssociation],"Beryllium"->\[LeftAssociation]"AtomicNumber"->4,"AtomicWeight"->9.01218`\[RightAssociation],"Boron"->\[LeftAssociation]"AtomicNumber"->5,"AtomicWeight"->10.8086`\[RightAssociation],"Carbon"->\[LeftAssociation]"AtomicNumber"->"6","AtomicWeight"->12.0107`\[RightAssociation]\[RightAssociation], "RawJSON"]

Export to a string:

ExportString[\[LeftAssociation]"Hydrogen"->\[LeftAssociation]"AtomicNumber"->1,"AtomicWeight"->1.00793`\[RightAssociation],"Helium"->\[LeftAssociation]"AtomicNumber"->4.00259`,"AtomicWeight"->2\[RightAssociation],"Lithium"->\[LeftAssociation]"AtomicNumber"->3,"AtomicWeight"->6.94141`\[RightAssociation],"Beryllium"->\[LeftAssociation]"AtomicNumber"->4,"AtomicWeight"->9.01218`\[RightAssociation],"Boron"->\[LeftAssociation]"AtomicNumber"->5,"AtomicWeight"->10.8086`\[RightAssociation],"Carbon"->\[LeftAssociation]"AtomicNumber"->"6","AtomicWeight"->12.0107`\[RightAssociation]\[RightAssociation], "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[\[LeftAssociation]"x"->{{1,2},{3,4}},"y"->{{Null}}\[RightAssociation], "RawJSON","Compact"->1]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {  ▪    ▪    ▪  }, {Related Formats:  (RefLink[ExpressionJSON, paclet:ref/format/ExpressionJSON])  ▪  (RefLink[JSON, paclet:ref/format/JSON])  ▪  (RefLink[UBJSON, paclet:ref/format/UBJSON])  ▪  (RefLink[XML, paclet:ref/format/XML])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Importing & Exporting, paclet:guide/ImportingAndExporting])}, {▪(RefLinkPlain[Listing of All Formats, paclet:guide/ListingOfAllFormats])}, {▪(RefLinkPlain[Systems & Utility Formats, paclet:guide/SystemsAndUtilityFormats])}, {▪(RefLinkPlain[Tabular & Spreadsheet Formats, paclet:guide/TabularAndSpreadsheetFormats])}, {▪(RefLinkPlain[Web Formats, paclet:guide/WebFormats])}})}})