Writting to JSON store from a Global Script
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
I would suggest searching the forums first. There is a thread for that and an answer by @CraigWeb
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.
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.
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;
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.