Message Boards Message Boards

0
|
8086 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

RawJSON vs JSON Import/Export Format

Posted 9 years ago

Since the release of version 10.2.0 I think, Import/Export and ImportString/ExportString functions accept a format parameter with the value of "RawJSON".

The differences between "JSON" and "RawJSON" format values are fully documented and can be referenced at Wolfram.

  • The "RawJSON" format identifies JSON objects with associations in the Wolfram Language.

  • The "JSON" format identifies JSON objects with lists of rules in the Wolfram Language.

My question is how did they come up with that name, i.e. "RawJSON", to specify that JSON objects will be represented with associations instead of rules ? How the prefix "Raw" is conceptually linked to associations here ? And if you look more into this then you find also that

by default, Import and Export use the "JSON" format for files ending in .json, rather than the "RawJSON" format.

I would expect the default behavior of importing a .json file without specifying the format parameter to be a STRING representation in Wolfram. It would also make sense, in my opinion, to name these two aforementioned format values to something like "JSONR" and "JSONA" for Rule and Association representations respectively. Otherwise this is a point where confusion start arising in different JSON representations, i.e. String, Rule and Association representations.

json = "{\"telephones\":{\"home\":\"2104566345\",\"business\":\"\2108856844\",\"mobile\":\"6974059256\"},\"firstName\":\"John\",\"\lastName\":\"Brown\",\"DOB\":\"1971-10-01\",\"age\":44}"

In[]:=   jsonString = ImportString[json, "Text"]
Out[]:=  {"telephones":{"home":"2104566345","business":"2108856844","mobile":\"6974059256"},"firstName":"John","lastName":"Brown","DOB":"1971-10-\01","age":44}

In[]:=   jsonRules = ImportString[json, "JSON"]
Out[]:=  {"lastName" -> "Brown", "telephones" -> {"home" -> "2104566345", "business" -> "2108856844","mobile" -> "6974059256"},"firstName" -> "John", "DOB" -> "1971-10-01", "age" -> 44}

In[]:=   jsonAssoc = ImportString[json, "RawJSON"]
Out[]:=  <|"telephones" -> <|"home" -> "2104566345","business" -> "2108856844", "mobile" -> "6974059256"|>,"firstName" -> "John", "lastName" -> "Brown", "DOB" -> "1971-10-01","age" -> 44|>

This is my answer attempting to justify my question above. Is there something more in "RawJSON" that I am missing here ? Despite the naming confusion, is the only difference between these two in using Rules vs Associations ?

It looks like the "Raw" prefix has to do something with the encoding. Let us focus on just two key,value pairs of the previous example, but this time using international characters, i.e. my name in Greek.

In[1]:=  json = "{\"firstName\":\"?????????\",\"lastName\":\"??????\"}"
Out[1]:= {"firstName":"?????????","lastName":"??????"}

In[2]:=  jsonString = ImportString[json, "Text"]
Out[2]:= {"firstName":"?????????","lastName":"??????"}

In[3]:=  jsonRules = ImportString[json, "JSON"]
Out[3]:= $Failed

In[4]:=  jsonAssoc = ImportString[json, "RawJSON"]
Out[4]:= <|"firstName" -> "?????????", "lastName" -> "??????"|>


Out[3] $Failed   
Import::fmterr: Cannot import data as JSON format. >>
SystemInformation["Kernel"][[1]]
Version?10.3.0 for Linux x86 (64-bit) (October 9, 2015)

Therefore I think there is great confusion here between encoding formats, e.g. ASCII, UTF-8, and representation constructs String, Rule, Association. I expect Mathematica architects will do something to clear this mess ;-)

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract