Message Boards Message Boards

GROUPS:

Package my output in as a json?

Posted 4 months ago
632 Views
|
2 Replies
|
2 Total Likes
|

Hello everyone. I have constructed a program which provides an output as a .txt. I have to email this output and the reciever has requested me to send it as a .json instead. I have attempted to simply save the output replacing .txt by .json using export as.

Export["savedWindXPythonJ.json", savedXPython];

However, I recive an error message Export::badval: The element Data contains invalid values.

Is there a way to save the variable to a json any answer is appreciated? I will attach the .txt version.

Regards.

Jaime.

Attachments:
2 Replies
Posted 4 months ago

You can export a list of rules or an association to JSON, but what you have is a different Mathematica expression (a long formula). Any Mathematica expression can be converted to JSON using the "ExpressionJSON" format:

Export["savedWindXPythonJ.json", savedXPython,"ExpressionJSON"];

I use this to save Mathematica expressions into a JSON based database.

UPDATE: I just noticed that your formula is not even a Mathematica expression. So I don't know what you are trying to do.

Hello Gustavo.

First of all, thanks for your answer.

I am trying to send a mathematical expression readable by pyomo to another person. I have constructed it in Mathematica and then adapted it to Python's grammar as

savedXPython = 
 Import["savedWindX.txt"] // 
  StringReplace[#, {"[" -> "(", "]" -> ")", 
     "t" -> "m.lammda[i]*180/np.pi", "u" -> "m.phi[i]*180/np.pi"}] &

Since I want this to be executed without having to do any work on the file. I have sent it as a txt but it seems that that is causing trouble. That is why I am trying to convert the expression to a way that can be read using pyomo.

Regards. Jaime.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract