Function Repository Resource:

ExportDeescapedJSON

Source Notebook

Export an expression to JSON without escaping forward slashes

Contributed by: Jeremy Michelson

ResourceFunction["ExportDeescapedJSON"][filename,json]

is equivalent to Export[filename,json,"JSON"] except that any "/" in a string is not escaped.

Details

Options supported by the JSON export format can be used.

Examples

Basic Examples (4) 

Export a string as JSON without escaping forward slash characters:

In[1]:=
file = ResourceFunction["ExportDeescapedJSON"]["deescaped.json", "https://www.wolfram.com"];
FilePrint[file]

It is valid JSON with or without the escapes:

In[2]:=
Import[file, "RawJSON"]
Out[2]=

Compare to standard JSON Export which escapes forward slashes:

In[3]:=
file = Export["escaped.json", "https://www.wolfram.com"];
FilePrint[file]

After importing they are equivalent:

In[4]:=
Import[file, "RawJSON"]
Out[4]=

Scope (2) 

Export an Association:

In[5]:=
FilePrint[
 ResourceFunction["ExportDeescapedJSON"][
  CreateFile[], <|"a" -> "a/file/path", "b" -> "another/file/path"|>]]

Export nested data:

In[6]:=
FilePrint[
 ResourceFunction["ExportDeescapedJSON"][
  CreateFile[], <|
   "fractions" -> <|"0.5" -> "1/2", "1.5" -> "3/2"|>|>]]

Properties and Relations (1) 

A backslash before a forward slash is left unchanged:

In[7]:=
FilePrint[
 ResourceFunction["ExportDeescapedJSON"][CreateFile[], "An \\/ example"]]
In[8]:=
FilePrint[
 ResourceFunction["ExportDeescapedJSON"][CreateFile[], "An \\\\/ example"]]

Possible Issues (2) 

ExportDeescapedJSON requires data to match the "RawJSON" formatting:

In[9]:=
ResourceFunction["ExportDeescapedJSON"][CreateFile[], {"a" -> 1}]
Out[9]=

Use Association instead of a list:

In[10]:=
ResourceFunction["ExportDeescapedJSON"][CreateFile[], Association@{"a" -> 1}]
Out[10]=

Version History

  • 1.0.0 – 24 March 2023

Source Metadata

Author Notes

Not every one realizes that the JSON specification supports escaping forwards slashes. This function can be used if the consumer of the JSON to be exported does not support the valid JSON created by Export.

License Information