6
$\begingroup$

It has been very recently, since the release of version 10.3.0 I think, that 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.

I think I made my point pretty clear, so I stop here.

$\endgroup$
  • 1
    $\begingroup$ I don't think that this question is something any regular user here on mathematica.stackexchange can give you an answer to. $\endgroup$ – Sascha Dec 23 '15 at 19:30
  • $\begingroup$ @Sascha there are many users in this community that I would not characterize them with the term "regular". They are exceptionally good in Mathematica and there are Wolfram Research employees here too. $\endgroup$ – Athanassios Dec 23 '15 at 19:37
  • 1
    $\begingroup$ Actually the differences are not fully documented, see at the end of this post. In the light of the behaviour I described in that post RawJSON is a bit more "raw" ... but then personally I would consider that behaviour a bug (while recognizing that this is debatable). $\endgroup$ – Szabolcs Dec 23 '15 at 20:40
  • 1
    $\begingroup$ I agree with Sascha that this is close to unanswerable unless the developer shows up here. But I also agree with Athanassios that the chosen naming seems to strongly suggest bigger differences than just Rules vs Associations (and I've been searching for those exactly because of the naming, without finding anything!). I also find the naming very confusing. $\endgroup$ – Szabolcs Dec 23 '15 at 20:44
  • 1
    $\begingroup$ Personally I would not close this question. I suspect that many people will have the same question. In the least, it would be useful to have some confirmation that "despite the naming the only difference between these two is using Rules vs Associations". Maybe you can rephrase the question and ask if that is really the only difference, given that the naming suggests something more. It is possible to give a useful answer to this question for someone who didn't design/implement the functionality. $\endgroup$ – Szabolcs Dec 23 '15 at 20:47
5
$\begingroup$
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 ;-)

$\endgroup$
  • 1
    $\begingroup$ Have you reported? $\endgroup$ – P. Fonseca Dec 24 '15 at 1:08
  • $\begingroup$ This post has been duplicated in Wolfram Community here $\endgroup$ – Athanassios Dec 31 '15 at 11:38
  • $\begingroup$ See also my answer here for Dataset $\endgroup$ – Athanassios Jan 30 '16 at 7:20
4
$\begingroup$

"RawJSON" returns a faithful and reversible representation of JSON, while "JSON" does not. Here's an example:

ImportString[#, "RawJSON"] & /@ {"{}", "[]"}
(* {<||>, {}} *)

ImportString[#, "JSON"] & /@ {"{}", "[]"}
(* {{}, {}} *)

As for the "Raw" name, the ExpressionJSON format gives a hint. Perhaps it's to distinguish from this.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.