You are reading a single comment by @d0773d and its replies. Click here to read the full conversation.
  • sensorReadings is already an object... and JSON.parse(aJSONString) expects a string as argument... ;-), therefore, expression var sensorObj = JSON.parse(sensorReadings); throws error (in Browser: ...Uncaught SyntaxError: Unexpected token o in JSON at position 1(ā€¦); what error is thrown in Espruino I cannot figure out right now, because have no Espruino (or emulator) at hand).

    This code (see second line)

    var sensorReadings = { "restempvalue" : 0, "phvalue" : 0, "ecvalue" : 0 };
    var sensorObj = JSON.parse(JSON.stringify(sensorReadingsĀ­));
    

    fixes your issue, but there is not point to first build a JSON string from sensorReadings object and second - right away - parse that JSON string back into an object... ;-)

    Plain assignment gets you there - because it is the same:

    var sensorObj = sensorReadings;
    

    Therefore, you can drop sensorReadings completely and just use:

    var sensorObject = { "restempvalue" : 0, "phvalue" : 0, "ecvalue" : 0 };
    

    It is a different story though when you want to write you readings onto a SD memory card, or transmit to wherever. For that you would

    A) for initialization

    1. create a sensorReadings object
    2. stringify the object using 'JSON.stringify() to a sensorReadingsAsString string
    3. write or send that sensorReadingsAsString string to the storage;

    B) for update that readings on the storage, you

    1. retrieve them from storage as sensorReadingsAsString
    2. parse it into the `sensorReadings' object
    3. update the desired property with value
    4. stringify the object using 'JSON.stringify() to a sensorReadingsAsString string
    5. write or send that sensorReadingsAsString string 'back' to the storage
  • @allobjects.. Ok, i think I get it. How do I access sensorObject to gather the values when using code:

    var sensorObject = { "restempvalue" : 0, "phvalue" : 0, "ecvalue" : 0 };
    

    Nevermind... I read over your posts again.. I was just a bit confused, before. Thanks for the informative response.

About

Avatar for d0773d @d0773d started