Select json.result

Hello,

I have the follow code working:

window.setInterval(function() {
requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + getFunc + "/?access_token=" + accessToken;
$.getJSON(requestURL, function(json) {
document.getElementById("correnteAtual").innerHTML = json.result + "mA";  //pega amperagem 
document.getElementById("correnteAtual").style.fontSize = "28px";
document.getElementById("degBoxId").value = parseInt(json.result);
});
     }, 1000);

the result is: {"data1":-0.6200,"data2":3333}

I want insert into variable1 that value -0.6200
and the variable2 = 3333

How can I do that?


I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

1 Like

Take a look at this tutorial, you may find it useful:
http://www.w3schools.com/js/js_json.asp

1 Like

If I am reading you right, json.result is itself another JSON with two items and you want to parse it, right?

If so you just need to call JSON.parse on the result you have to recursively parse the next level and then use the dot notation “.” to access the fields:

var parsedResult = JSON.parse(json.result);
document.getElementById("FirstDataId").value = parsedResult.data1;
document.getElementById("SecondDataId").value = parsedResult.data2;

I’m guessing this is what you want? I thiiiink tou can access any of the variables from the string with its name like json.namehere

window.setInterval(function() {
  requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + getFunc + "/?access_token=" + accessToken;
  $.getJSON(requestURL, function(json) {
    document.getElementById("correnteAtual").innerHTML = json.data1 + "mA";  //pega amperagem 
    document.getElementById("correnteAtual").style.fontSize = "28px";
    document.getElementById("degBoxId").value = parseInt(json.data2);
  });
}, 1000);

Note: You don’t need to perform JSON.parse with this one as you are using $.getJSON which is automatically set to parse the result.

Hello Mr. bko,

I'm using your code to get the values of the spark and puts them into two variables

In my spark file I have that code

sprintf(resultstr, "{\"data1\":%.4f,\"data2\":%d}", val, state); 

I changed the javascript to:

requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + getFunc + "/?access_token=" + accessToken;
$.getJSON(requestURL, function(json) {

var parsedResult = JSON.parse(json.result);

document.getElementById("FirstDataId").value = parsedResult.data1;
document.getElementById("SecondDataId").value = parsedResult.data2;
    });
      }, 1000);
    </script>
<body>
    <P>Amper: <span first="SecondDataId"></span><br>
    <P>Amper: <span second="FirstDataId"></span><br>

But I can see only first and second, without json data

first
second


I've edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

Iv4n

Hello, I tried your code but I receive “Indefined”

in my spark code I have that line:
sprintf(resultstr, “{“data1”:%.4f,“data2”:%d}”, val, state);

Hi @rbbahia

I just picked the names for the HTML fields “FirstDataId” and “SecondDataId” out of the air at random–you should use the names of fields you have defined in the HTML part of your web page.

If you use the Javascript debugger in your browser, you should be able to tell if parsedResult has the right value–what you are doing should be correct.

You can have a look at my JSON tutorial for publishing too:

Hello rbbahia, try this one… it might work but I haven’t tested it:

window.setInterval(function() {
  requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + getFunc + "/?access_token=" + accessToken;
  $.getJSON(requestURL, function(json) {
    document.getElementById("correnteAtual").innerHTML = json.result.data1 + "mA";
    document.getElementById("correnteAtual").style.fontSize = "28px";
    document.getElementById("degBoxId").value = parseInt(json.result.data2);
  });
}, 1000);

Hello bko,

I already debug with javascrip debbuger, no errors found.

Hello Iv4n,

I received “indefined” too.

Maybe you could show us a screen shot of the error–it is not clear what is undefined here and I think it must be something else besides the JSON we are working on here.

ok

That is the spark code

You need to take the ampersand out this line:

Spark.variable("result", &resultstr, STRING)

should be

Spark.variable("result", resultstr, STRING)

I tried this code but I get the same message “Undefined”

The data is returned correctly, so now you just have to parse it in your JavaScript.

{
  "cmd": "VarReturn",
  "name": "result",
  "result": "{\"data1\":-0.7100,\"data2\":1}",
  "coreInfo": {
    "last_app": "",
    "last_heard": "2015-03-05T20:20:59.599Z",
    "connected": true,
    "last_handshake_at": "2015-03-05T20:20:43.457Z",
    "deviceID": "54ff6e066678574911430567"
  }
}

Yes my code copies the values to a variable in javascript, the problem is that I can not use them separately in a variable date1 and date2 in another variable

Could you paste the full HTML file here, then we can take a look at it? shouldn’t be that hard.

Thank you for the patience. I found another solution.

using substr to get just selected data.

thank you very much

Glad to hear you’ve got it working. I would however highly recommend trying out the JSON way, since that should be a lot easier to work with.

Chrome Console makes Javascript fun!

3 Likes