Group Abstract Group Abstract

Message Boards Message Boards

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

RawJSON vs JSON Import/Export Format

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

In[]:=   jsonString = ImportString[json, "Text"]
"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