# Export & Import Association with Lists and Rationals without ExpressionJSON

Consider following association:

sym = <|
"p6mm" -> <|
"e" -> {{x, y}, {-y, x - y}, {-x + y, -x}, {-x, -y}, {y, -x + y}, {x - y, x},
{-y, -x}, {-x + y, y}, {x, x - y}, {y, x}, {x - y, -y}, {-x, -x + y}},
"d" -> {{x, -x}, {x, 2 x}, {-2 x, -x}, {-x, x}, {-x, -2 x}, {2 x, x}},
"c" -> {{1/2, 0}, {0, 1/2}, {1/2, 1/2}},
"b" -> {{1/3, 2/3}, {2/3, 1/3}},
"a" -> {{0, 0}}
|>
|>


I would like to store this in an external file preferably json format and import this to a Mathematica association as shown. Possibly converting the general positions into a matrix with

vectorToTransMatrix[v_List] := If[ReplaceAll[v, {x -> 0, y -> 0}] == {0, 0},
Map[Coefficient[#, {x, y}] &, v], {}];
vectorToTransMatrix[{-y, x - y}]
{{0, -1}, {1, -1}}


Issues are with ordinary json that Rationals are converted to floating point which I absolutely do not want. Otherwise I would not ask this question.

I know there is the format ExpressionJSON which allows for round-tripping. However, the external files are not as condensed as I wish they would be.

I guess some postprocessing when reading and writing is necessary. I do not need full support for all Mathematica features. Following would be a great first step:

Mathematica association <-> Json dictionary
Mathematica list <-> Json list
Mathematica rational <-> Json string

My Mathematica version is 11.2.

• Since it comes from MMA and is meant for MMA, what is the point? Just export Compress-ed .txt file or .wl. – Kuba May 4 '18 at 13:21
• Thanks for your thoughts. The data should be human readable and actually not only useful for MMA. Main reason is that I want to enter larger junks of data similar to this one. MMA is not really superior compared to texteditors such as vim when it comes to entering larger data sets. However, when I have to add all Heads it is also in vim more painful to do this. Another point is that maybe someday I also want to use the data in Sympy or another tool where I can reuse it without much to do. – Hotschke May 4 '18 at 13:33
• @Hotschke OK so perhaps just export as plain text? You can do a round trip: ToExpression@Import[Export["text.txt", sym]] === sym returns true. – MarcoB May 4 '18 at 14:11
• I guess I go with text export for now. I usually prefer structured formats where I can use other tools to work with them. Json can be read by almost every language; it can be checked for validity; formatting a file is easier; command line interaction e.g. with stedolan.github.io/jq is possible, etc. I think you get the picture. If I want to use only MMA, I could move it to .m file and import this one. – Hotschke May 4 '18 at 14:21
• Your association has undefined symbols in it, I have no idea how you want to serialize something like x-y. But once you figure that out, then you can do something like Export[filename, sym, "JSON", "ConversionRules" -> {x_Rational :> ToString[FullForm[x]]}] – Jason B. May 4 '18 at 14:48