JSON in Snap!?

I am working with JS in Snap!, however, that JS triggers a ringified script from a variable with an argument, and that argument should be something that resembles JSON. Any help? For now I can only think of using two lists in a list, one for keys and the other for values. But what if the values are also JSON? You never know.

Use Advanced Help with Snap!, since the average user of Snap! wouldn't normally use JS.

So do I have to remake this topic in there?

No, you can edit the post and its category. :slight_smile:

Did exactly that. :stuck_out_tongue:

If you would like to convert json data to a list, you can use this block.
1000446086

Now that's swag. :astonished:

the other way around:
untitled script pic

So like list to JSON? Could be kind of useful too ngl

Also, how do you call ringified functions from args in JS?

it should just work normally, what do you mean? can you send a script pic?

holy moly script pic
And onMsg is...
holy moly script pic (1)

The JS logs me a Context object

1: the URL part doesn't do anything, you arent using it in the JS function.
now, the primitive console log block unfortunately does not work in user mode, otherwise I would've told you to use that.
2: you cannot console.log a ringified block, the CONTEXT element does have all the data for the block though.

  1. I will use the URL soon, WIP.
  2. wdym? I just log it for me to analyse. Of course it's not gonna popup graphically like in a say block

so how is it not working?

I want to call the ring of the blocks in JS like the call/run blocks.

If your goal is to parse json, then you can do it in snap and pass the result in, rather than passing in a ring. However, if you need to parse json in javascript, it would be better to use the javascript function instead (because it really is easier than trying to run the snap script, and convert the result into something usable in js).

JSON.parse('{"result":true, "count":42}')

And if you want to convert a js list to a snap list (to be able to use the resulting data in snap), just do

return new List(list)

Yeah, but suppose I got a message from my WebSockets, and most of my message-handling functionality is in Snap as a ring, but the message got received in JS. So how do I call the ring like in a call/run block with an argument of the (un)parsed JSON?

Ah, I see. I didn't look at the url to see if you were using we sockets.

I personally can't really figure out the best way to call a snap script inside javascript (without the javascript function block reporting the result of the script that is ran), so I think you probably should just broadcast a message instead.

You can broadcast a message in snap just by running

let ide = this.parentThatIsA(IDE_Morph)

ide.broadcast(message, null, 'data')

The first argument in the ide.broadcast() method is the message, the second one is a callback function that runs after all the received messages have ran (basically just a broadcast and wait), and then the last argument is optional data that can be sent with a message, which you can get by expanding the "when I receive" block.

And of course you may want the message to be user defined, specifically in case you have multiple websockets.