JSON string to GAMA map #72
-
Hi, I have a message I get from a Python TCP server as a string in GAMA. The message is actually a Python dictionary turned to a valid JSON string using json.dumps function. I have seen many issues and discussions on reading JSON files into GAMA, but unfortunately nothing on JSON strings. As I am not in a position to dump every message into a file and then read it, these discussions do not help me here. Here is the string I have: What I currently run in GAMA:
Does anyone know how to read the string above into a GAMA map? Keep in mind that I can manipulate the message in Python and send it as a nested list or any other format if it makes it easier to handle in GAMA. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 0 comments 7 replies
-
If anyone runs into a similar issue, I managed to create the map like I wanted by sending a nested list string to GAMA, evaluating it with GAMA's eval function and then creating a map using two lists (1st for keys and 2nd for values). Here is the string I sent from Python: The code I ran in GAMA to create a map object:
That said, if there is an obvious way to turn JSON string to map in GAML, please let me know. |
Beta Was this translation helpful? Give feedback.
-
Hi
Maybe we could add a simple operator to transform JSON string into a map,
as it seems we have everything behind
Cheers
Benoît
Le mer. 30 nov. 2022 à 03:24, Baptiste Lesquoy ***@***.***> a
écrit :
… I think you went in the right direction with the eval_gaml approach, you
can simplify it a bit by formatting your input string differently. If you
use the format given by the output of your script, you won't need to
convert lists of strings, you can directly evaluate the string as a map:
map<string, string> m <- eval_gaml('["people3"::"people5", "Festungsbahn Talfahrt XML"::"Final"]');
write m;
write m["people3"];
—
Reply to this email directly, view it on GitHub
<https://github.com/gama-platform/gama/discussions/3539#discussioncomment-4269818>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAL3WSRYP5DHOQBRXSFRNTDWK23FHANCNFSM6AAAAAASOGYHBM>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Dear Ivan,
Alexis just pushed a new commit, that may fit with your initial need.
Cheers
Benoit
*"I have quickly added this case to the map casting operator. The output is
the same as the one in Json files : if you feed it with a direct map it
will output this map, otherwise it will output a map with a unique key
named "contents", under which the result of the parsing is stored. If the
string is malformed, it will raise an exception.*
*To summarize:*
- write map('{"key1" "AA, "key2": 100}'); ==> Exception
- write map('{"key1": "AA", "key2": 100}'); ==>
map(['key1'::'AA','key2'::100])
- write map('["key1": "AA", "key2": 100]'); ==>
map(['key1'::'AA','key2'::100])
- write map('[{"key1": "AA", "key2": 100}]'); ==>
map(['contents'::[map(['key1'::'AA','key2'::100])]])
"
Le mer. 30 nov. 2022 à 13:53, Ivan Majic ***@***.***> a
écrit :
… I assumed it would already be possible when I saw this older discussion: #1900
(comment)
<https://github.com/gama-platform/gama/issues/1900#issuecomment-249096236>
.
Following the example from this comment, I tried loading the contents of
the message (JSON string) into a map, similar to how they loaded contents
of a JSON file to a map. (that was the original code I posted above)
It didn't work for me but it's probably because the contents of a JSON
file aren't read into GAMA as a regular string?
—
Reply to this email directly, view it on GitHub
<https://github.com/gama-platform/gama/discussions/3539#discussioncomment-4273464>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAL3WST7VMB7RIKABSNJ3QTWK5E5LANCNFSM6AAAAAASOGYHBM>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
If anyone runs into a similar issue, I managed to create the map like I wanted by sending a nested list string to GAMA, evaluating it with GAMA's eval function and then creating a map using two lists (1st for keys and 2nd for values).
Here is the string I sent from Python:
'[["people3", "people5"],[ "Festungsbahn Talfahrt XML","Final"]]'
The code I ran in GAMA to create a map object:
That said, if there is an obvious wa…