Skip to content

ExportString

The inverse of ImportString: serialises an expression in a given format and returns the result as a string. Wolfram supports many named backends ("CSV", "JSON", "PNG", etc.); Woxi currently implements a subset.

Only machine numbers are written bare; every other value is quoted, and a compound one that CSV cannot represent becomes a -Head- placeholder:

$ wo 'StringTrim[ExportString[{{a, 1, True, x + y}}, "CSV"]]'
"a",1,true,"-Plus-"

"String" gives the expression's own text:

$ wo 'ExportString[{{1, 2}}, "String"]'
{{1, 2}}

A value JSON cannot represent fails the export rather than being approximated:

$ wo 'ExportString[<|"a" -> Pi|>, "JSON"]'

Export::jsonstrictencoding: Expression Pi cannot be exported as JSON.
$Failed

Symbolic XML is written back out as markup:

$ wo 'ExportString[XMLElement["a", {"x" -> "1"}, {"t"}], "XML"]'
<a x='1'>t</a>

Elements without children are self-closing, and element-only content is indented one space per level:

$ wo 'ExportString[XMLElement["a", {}, {XMLElement["b", {}, {}]}], "XML"]'
<a>
 <b />
</a>