Json from PHP to agent to device and then decode

I’m sending a json from a php script to my agent url, the url check if a particular key is good and then send the same json to the device,

How can I parse the json in the device… ?

PHP code :

`$_SESSION['config'] = array(    
    "secret"                       => $_SESSION['current_node_url'], 
    "I1Enabled"                 => xss_cleaner($_POST['I1Enabled']));
    $data_string = json_encode($_SESSION['config']);                                                                                                                                                                                                      
    $ch = curl_init('https://agent.electricimp.com/'.$_SESSION['current_node_url']);                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                                                                                                                                           
    $result = curl_exec($ch); `

Agent code

`http.onrequest(function(request, response){
  try 
  {
    // decode the json - if it's invalid json an error will be thrown
    //server.log("body" + request.body);
    local data = http.jsondecode(request.body);
    
    if ("secret" in data) {
        if(data.secret == knowngoodsecret )
        {
            response.send(200, "Got it ");
            device.send("newconfig", request.body);
            server.log("new config sent to device"); 
        }
        else
        {
            server.log("secret is bad"); 
            response.send(400, "invalid request");
        }
    }
    
  } 
  catch (e) 
  {
    response.send(500, "Invalid JSON string");
  }
  
});`

Device code

`function updatedeviceconfig(json) 
{
    server.log("received new config on device " + json);
  
}
 
// When we get a 'pong' message from the agent, call returnFromImp()
agent.on("newconfig", updatedeviceconfig);
`

I’m interested to know the code to put in my device to find the value of each element that are in the original array in my php script, for example I1Enabled …of course my real array is much much bigger ( 150 + parameters ) than what is shown here.

Let the agent do the work for you. There is no native way of getting the device to parse the json (although I wish there were). You can include your own json decoder in the device, but it will run 1000 times slower than http.jsondecode() in the agent.
Just send the already decoded object down to the device. It may seem silly, but the end result is that the agent takes a JSON string an converts it to a Squirrel object, device.send then converts that object to a BSON (I think) string in the agent, sends it to the device, which then reconstitutes the BSON into a Squirrel object again.

Thus, instead of:
device.send("newconfig", request.body);
send the decoded object to the device with:
device.send("newconfig", data);

You have 150 parameters within a PHP SESSION? And then those 150 to the device? Are you able to tell us more about what your project is? I’m thinking that is a lot of SESSION variables (or big array), and what could they all be for. My curiosity is killing me.

I’ve found that if you use
#require "JSONParser.class.nut:1.0.0"

in the device

and

`
function updatedeviceconfig(json) 
{
    server.log("received new config on device " + json);
    data <- JSONParser.parse(json);
    //if("I1Unit" in data) { server.log(" got new unit "+ data.I1Unit ); }
    foreach (key, value in data) {
    server.log("Key: " + key + " has the value: " + value);
}
}
 
// When we get a 'pong' message from the agent, call returnFromImp()
agent.on("newconfig", updatedeviceconfig);
`

data.xxxx work to reach all key and value in the JSON, wich is great

mlseim Yep, I know, its a lot, but I absolutely need all of those for my project. I can’t discuss about the purspose of my device for now, but let say that its a complex system that include a mysql database, 2 php site and the possibility of all my imp working like if there is only one imp, and this can scale without limit of imps :wink: