Inverse of JSObject.to_dict() ?

25 views
Skip to first unread message

ben levitt

unread,
Mar 26, 2023, 2:23:06 AM3/26/23
to bry...@googlegroups.com
I'm using a js library in my brython code that wants to take JS dictionary/objects.  Passing brython dictionary objects doesn't work, and I'm having trouble figuring out how to convert a python dict into a JSObject in my brython code, such that I could pass into this JS library.

I've found I can use:
import json
my_dict = {'a': 1, 'b': 2}
js_obj = window.JSON.parse(json.dumps(my_dict))

but that seems pretty inefficient.  Also, note that I need to use the js version of JSON.parse() from window, not the brython version (javascript.JSON.parse()), since that one returns a python dict.

Is there a cleaner, faster way to do this?  I'd love something like: javascript.to_jsobj(python_dict)

Thanks!
Ben

José María Sánchez Ruiz

unread,
Mar 26, 2023, 5:33:34 AM3/26/23
to bry...@googlegroups.com
You can use window.Object.new(your_dict)

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAHyr3YjODaAzmpu1fjvFMaTAmGwF%2BcdVRkn0SvuR%3DJv9DRC8ug%40mail.gmail.com.

ben levitt

unread,
Mar 26, 2023, 10:59:53 AM3/26/23
to bry...@googlegroups.com
Thank you!  That's exactly what I was looking for.

Ben

Reply all
Reply to author
Forward
0 new messages