Frappe Custom Script - JSON.stringify not working

Hello all!

I have been attempting to create a custom script, and I need to pass a dictionary from the client-side Javascript (in the webpage) through to a whitelisted function in Python.

Usually I would convert that to a string using JSON.stringify(java_dict), then convert that back into a dictionary in Python. For some reason though, JSON.stringify(java_dict) is returning a completely empty string.

In an attempt to isolate the error, I made a simplified example that fails in the same way:

var obj = { name: "John", age: 30, city: "New York" };
var myJSON = JSON.stringify(obj);
frappe.msgprint(myJSON);

This “myJSON” string is an empty string.

Furthermore, when going to the webpage the javascript is embedded in and running the console command “JSON.stringify(java_dict)”, I get exactly what I expect - a string in JSON format that described the dictionary.

Any help would be appreciated!

  • ensure that you’r whitelist function returen a dict
  • please share the entire code for more reference

Hello Antony,

Thanks for your response!

You seem to be misunderstanding slightly - it’s not to do with what the whitelisted function returns, but with what gets passed into the whitelisted function. My issue is with the function “JSON.stringify” not returning what is expected. The first paragraph of my initial post is only to explain why I need the dictionary in JSON format - the whitelisted python function is working as intended.

You can replicate this issue by constructing a “Web Page” item, with the following fields filled:

Content > Content > Main Section (HTML):

<!DOCTYPE html>
<button id = "testbutton" onclick="buttonClick()">Click Me!</button>

Scripting > Script > Javascript:

function buttonClick()
{
    var obj = { name: “John”, age: 30, city: “New York” };
    var myJSON = JSON.stringify(obj);
    frappe.msgprint(“Here 1!”);
    frappe.msgprint(myJSON);
    frappe.msgprint(“Here 2!”);
}

The above code creates a dictionary called “obj”, attempts to convert it into a JSON string, then prints it in a dialogue box. I have added some messages “Here 1!” and “Here 2!” to show that frappe.msgprint() is working as intended, while myJSON (the JSON string) is blank.

I hope this helps!