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.
How to create and save Json Data in the JSON Store via a global script
-
Hi folks, whilst writing some config for a job I thought everyone who likes to use the Json Store might appreciate this global script I threw together:
function JsonObjectInstance(xid) { this.jsonXid = xid; this.jsonDataDao = com.serotonin.m2m2.db.dao.JsonDataDao.instance; this.jsonDataVo = {}; this.createNewInstance = function() { print("createNewInstance") //it's not instantiated, create a new one this.jsonDataVo = new com.serotonin.m2m2.vo.json.JsonDataVO; var mangoPermissions = new com.infiniteautomation.mango.permission.MangoPermission(2); //set to 1 for superadmin this.jsonDataVo.setXid(this.jsonXid ); this.jsonDataVo.setName(this.jsonXid ); this.jsonDataVo.setReadPermission( mangoPermissions );//superadmin by default this.jsonDataVo.setEditPermission( mangoPermissions );//superadmin by default var objectMapper = com.serotonin.m2m2.Common.getBean(com.fasterxml.jackson.databind.ObjectMapper.class, "daoObjectMapper"); var newJsonData = objectMapper.readTree('{}'); //default '{}' // If objectmapper is not used before saving, all objects get saved as an array. this.jsonDataVo.setJsonData(newJsonData); // this.jsonDataVo.save(newJsonData); //Mango3 this.jsonDataDao.insert(this.jsonDataVo);//mango4 }; //GET JSON STORE this.getJsonStore = function(data) { this.jsonDataVo = this.jsonDataDao.getByXid( this.jsonXid ); if(!this.jsonDataVo || this.jsonDataVo===null) { this.createNewInstance(); if(data) { var objectMapper = com.serotonin.m2m2.Common.getBean(com.fasterxml.jackson.databind.ObjectMapper.class, "daoObjectMapper"); var newJsonData = objectMapper.readTree('{}'); //default '{}' //If objectmapper is not used before saving, all objects get saved as an array. this.jsonDataVo.setJsonData(newJsonData); // this.jsonDataDao.save(this.newJsonData); //Mango3 this.jsonDataDao.insert(this.jsonDataVo);//mango4 } } }; this.getJsonStore(); this.getJsonData = function() { if(!this.jsonDataVo) { this.getJsonStore(); } var jsonString = ""; if(this.jsonDataVo.getJsonData) jsonString = this.jsonDataVo.getJsonData(); return jsonString; }; this.setJsonData = function(obj) { var objectMapper = com.serotonin.m2m2.Common.getBean(com.fasterxml.jackson.databind.ObjectMapper.class, "daoObjectMapper"); var newJsonData = objectMapper.readTree(JSON.stringify(obj)); //default '{}' this.jsonDataVo.setJsonData(newJsonData); // this.jsonDataDao.save(this.jsonDataVo) //Mango3 this.jsonDataDao.update( this.jsonDataVo , this.jsonDataVo);//mango4 }; }If the json store object doesn't exist, it'll create it. Refer to example below to use...
var jsonObj = new JsonObjectInstance("VM_LIST"); jsonObj.setJsonData({key:"Tes2t",value:9000}) print(jsonObj.getJsonData()) // {"key":"Tes2t","value":9000}Fox
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login