• Recent
    • Tags
    • Popular
    • Register
    • Login

    Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

    Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website Mango 5 Documentation Website

    Writting to JSON store from a Global Script

    How-To
    json scripting
    2
    6
    1.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      marganellim
      last edited by

      Hi everyone. New to Mango so bare with me.
      I'm trying to figure out how to write data to the JSON store from a global script.
      The intent is to pass some variables to a global script, have it massage that data and write some JSON to the JSON store.

      If someone could point me toward a document describing how to access the JSON store from a script. Thanks so much

      1 Reply Last reply Reply Quote 0
      • ThomasEinastoT
        ThomasEinasto
        last edited by

        Hi

        I would suggest searching the forums first. There is a thread for that and an answer by @CraigWeb

        @craigweb said in Calling JSON store item in scripting datasource:

        Hi @ThomasEinasto

        Here is some extracts of a scripting data source I did recently minus the logic I did on the JSON.

        var dao = com.serotonin.m2m2.db.dao.JsonDataDao.instance;
        var objectMapper = com.serotonin.m2m2.Common.getBean(com.fasterxml.jackson.databind.ObjectMapper.class, "daoObjectMapper");
        
        var jsonDataVo = dao.getByXid('8b203a82-166a-4963-8dc5-17bba149e28b');
        var dataAsString = jsonDataVo.jsonData.toString();
        var data = JSON.parse(dataAsString);
        

        You can now edit the data object as you would normally in JS

          var newJsonData = objectMapper.readTree(JSON.stringify(data)); 
         //If objectmapper is not used before saving, all objects get saved as an array.
          jsonDataVo.setJsonData(newJsonData); 
          dao.save(jsonDataVo) 
        

        To save back to the JSON store.

        1 Reply Last reply Reply Quote 0
        • M
          marganellim
          last edited by

          Sorry, I did dig up this post but was confused by the data access object and serotonin stuff. I assumed this was some sort of special case.
          I will experiment with this more and in future be a bit more thorough in my searching.
          Thanks

          1 Reply Last reply Reply Quote 0
          • ThomasEinastoT
            ThomasEinasto
            last edited by

            Dao and serotonin stuff are Mango backend in Java. As Scripting in Mango allows users to write either in Java or Javascript you are able to initiate backend functions also easily. This in return allows users to search around IAS Github but without documentation. e.g. you can get points and set them also using Java methods if you know how to access them instead of adding points to context in the DS.

            1 Reply Last reply Reply Quote 0
            • M
              marganellim
              last edited by

              Hello
              I am getting an error with this script

              "com.serotonin.m2m2.rt.script.ScriptError: TypeError: dao.save is not a function at line: 49"
              

              dao is defined above as

              var dao = com.serotonin.m2m2.db.dao.JsonDataDao.instance;
              
              1 Reply Last reply Reply Quote 0
              • ThomasEinastoT
                ThomasEinasto
                last edited by

                Can you show your whole method instead of the error? Cant really help otherwise. Also version of Mango would also be helpful to know as 4.x has some major changes in scripting env.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post