×

Please give details of the problem

Skip to content

How to inject a JSONObject in a Web interface variables

You have a json variable my_json like:

{  
    "firstname" : "John",  
    "address" : "Paris",  
    "lastname" : "Smith " 
}

use the following code:

for (var i in my_json) {  
    var special_value = false;  
    if (typeof(my_json[i]) == "object") {  
        //if my_json[i] is an array or json  
        if (my_json[i].length != undefined) {  
            //my_json[i] is an array  
            if (my_json[i].length == 0) {  
                //empty array []  
                RMPApplication.setVariable(i, "[]");  
                special_value = true;  
            }  
        } else {  
            //my_json[i] is a json  
            var nb_keys = 0;  
            for (var j in my_json[i]) {  
                nb_keys++;  
            }  
            if (nb_keys == 0) {  
                //empty json {}  
                RMPApplication.setVariable(i, "{}");  
                special_value = true;  
            }  
        }  
    }  
    if (!special_value) {  
        RMPApplication.setVariable(i, my_json[i]);  
    }  
}