Message Boards Message Boards

Wolfram language code to JSON with Association and ExpressionJSON

GROUPS:

This tip may be useful for the people searching to convert the Wolfram code to the JSON file. This small method was developed answering to my friends working to the system development project on the computer code to JSON based database. His question is how to convert the Wolfram code to JSON, and the last proposal can satisfied his requirement.

The method is composed of Association, RuleDelayed, and ExpressionJSON as follows.

json = Association[
   in[1] :> (i = 2),
   in[2] :> (text = ToString[i] <> "xyz"),
   in[3] :> i^3
   ];

Parentheses will be used instead of Set function. Then we can write a file converted to JSON.

Export["test.json", json, "ExpressionJSON"];

After that we can import the JSON file and can execute the code.

json = Import["test.json", "ExpressionJSON"];
json[in[1]]
-> 2
json[in[2]]
-> 2xyz
json[in[3]]
-> 8

Enjoy.

POSTED BY: Hirokazu Kobayashi
Answer
25 days ago

Group Abstract Group Abstract