working with strings, messages, symbols and escaping special characters

jpenca's icon

ok so I'm not really used to Max. programming in other things tho.
gotta say, Max is extremely weird and frustrating for working with strings.

I'm working on some JS code which handles JSON data. now, all I want to do is to be able to type/create JSON strings inside Max, such as:

{
  "a" : 1,
  "b" : 2
}

for example, in a textedit view, or in a message box or SOMETHING, to be able to send this as a string to my JS object. But all the things I've tried will just eat the quotes because, hey, why not, who needs escaped quotes, right?

the string that I would expect here is:"{\"a\":1,\"b":2}"

how do I do this?

I know that Max can do it, e.g. if you use the [maxurl] object to get JSON from some API, and the JSON is encoded in the body, you can [dict.unpack body:] to receive a properly escaped string.

please, it is driving me NUTS.
thanks.

jpenca's icon

ps: if it can include whitespace characters, aka preserve formatting, that would be FUN. but I can live without that. just need proper strings from JSON.

only way I can come up with right now is to use another JS script and create objects there, then just outlet(0, JSON.stringify(myObject));.

11OLSEN's icon

I can put this into a message box
"{
\"a\":1,
\"b\":2
}"
or this {\"a\":1\,\"b\":2} without loosing any quote.?

Federico-AmazingMaxStuff's icon

Sorry for respawning old post, but how can I do this in a procedural way? I have a text inside the text message with semicolon and colons. When sending this into "tosymbol" all the punctuation disappears. How can I keep the punctuation? Also some JS method?