×

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:

1
2
3
4
5
{  
 "firstname" : "John",  
 "address" : "Paris",  
 "lastname" : "Smith " 
}

use the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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]);  
 }  
}