10

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.

Kuba
  • 136,707
  • 13
  • 279
  • 740
Athanassios
  • 1,291
  • 10
  • 22
  • 1
    I don't think that this question is something any regular user here on mathematica.stackexchange can give you an answer to. – Sascha Dec 23 '15 at 19:30
  • @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. – Athanassios Dec 23 '15 at 19:37
  • I did't mean "regular" in the sense of "not qualified" rather I doubt that anybody here has insight into why somebody at Wolfram Research chose to name this function/option "RawJSON" and not "Bob" – Sascha Dec 23 '15 at 19:41
  • Someone voted down my question. So I am asking him to get in the trouble and explain why ? Does not this question show research effort ? Is not this question useful or clear ? – Athanassios Dec 23 '15 at 19:43
  • @Sascha any Mathematica user expects to find reasonable names for anything in Wolfram Language. In fact I am well aware that research staff in Wolfram are meticulous in naming and identifying things. In this case have they failed in naming properly argument values ? I want to share my opinion on this issue with other users here and see what they think. Is it anything wrong with this ? – Athanassios Dec 23 '15 at 19:52
  • I downvoted the question because I think it is heavily opinion-based and not within the scope of the website to discuss minor naming conventions (might be better to raise the issue with Wolfram Research's support if you really feel this is an issue that needs change) – Sascha Dec 23 '15 at 20:14
  • 1
    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). – Szabolcs Dec 23 '15 at 20:40
  • OK @Sascha, thank you for the justification. You do have a point here. I have to admit that my question is opinion-based and leads to discussion and not to a solution in a problem. But it also clarifies matters with Importing/Exporting JSON in Mathematica and I think it should stay. This "RawJSON" has been added very recently and for me it was confusing. So I thought to share my experience with others on this. – Athanassios Dec 23 '15 at 20:42
  • 2
    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. – Szabolcs Dec 23 '15 at 20:44
  • 1
    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. – Szabolcs Dec 23 '15 at 20:47
  • @Szabolcs, excellent point, I did experience this UTF-8 default encoding problem for JSON files as described in this post and I did not know how to explain it. – Athanassios Dec 23 '15 at 21:18

2 Answers2

6

"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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
5
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 ;-)

Athanassios
  • 1,291
  • 10
  • 22