Adding new object to existing object

Naga Ravindra R
Kilo Sage

Hi all,

 

I have four variables. category, sub category, service and service offering.

Category and sub category are single line text. 

Service and Service offering are reference to respective tables.

 

I have an existing JSON object which is stored in a system property.

 

JSOn is like below:

 

 

{
    "apps": {
        "Corporate - Learning Platform": {
            "service": "18a39f9dc34a2190a0135c3ce00131ad",
            "offering": ""
        },
        "Corporate - Oracle EPM (Enterprise Performance Management)": {
            "service": "71b26be7479c6d102bb635a2e36d435c",
            "offering": ""
        },
        "Corporate - Oracle ERP (Enterprise Resource Planning)": {
            "service": "7db22be7479c6d102bb635a2e36d4385",
            "offering": ""
        },
        "Corporate - ATS -Recruitment Platform": {
            "service": "0542e69dc3462190a0135c3ce0013134",
            "offering": ""
        },
},

}

 

in above JSON, apps is category and Corporate is sub category.

 

 

Requirement: when end user fills all four variables and submits the catalog item. I have to create a new JSON inside the JSON. Similar to apps.

 

I'm calling a script include from flow using action. Inside action I'm passing all variables.

I have tried like below:

 

var jsonObject = new GlideRecord('sys_properties');
jsonObject.get('858b9c2847c6ed502bb635a2e36d432b');
var jsonValue = jsonObject.getValue('value');

var js1 = JSON.parse(jsonValue);

//
var js4 = JSON.stringify(js1);

jsonObject.value = js4;
jsonObject.update();

 

Please help me on how to add an new object in this system property.

 

For example, if end user submits variable like:

category: servicenow

subcategory: developer

service: 89677iu4hkhoiusknfukhfouehoh

service offering: ilhuihur79297skfiowur09328094

 

I have to create a new object. Any help?

1 ACCEPTED SOLUTION

@Naga Ravindra R Update your code as follows.

 

updateJSON: function(addCat, addSubCat, addService, addOffering) {


        var jsonObject = new GlideRecord('sys_properties');
        jsonObject.get('858b9c2847c6ed502bb635a2e36d432b');
        var jsonValue = jsonObject.getValue('value');

        var js1 = JSON.parse(jsonValue);

        var innerJSON = {};
        innerJSON["service"] = addService;
        innerJSON["offering"] = addOffering;
        var someObj = {};
        someObj[addSubCat] = innerJSON;
        js1[addCat] = someObj;
        var js3 = JSON.stringify(js1);
        jsonObject.value = js3;
        jsonObject.update();
    },

 

Please don't forget to mark my answer correct and helpful.

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@Naga Ravindra R Please update your code as follows.

 

var jsonObject = new GlideRecord('sys_properties');
jsonObject.get('858b9c2847c6ed502bb635a2e36d432b');
var jsonValue = jsonObject.getValue('value');
var js1 = JSON.parse(jsonValue);
var innerJSON = {};
innerJSON["service"] = '89677iu4hkhoiusknfukhfouehoh';
innerJSON["offering"] = 'ilhuihur79297skfiowur09328094';
js1['servicenow']={'developer':innerJSON};
var js4 = JSON.stringify(js1);
jsonObject.value=js4;
jsonObject.update();

 

Please mark this answer helpful and correct if it manages to address your requirement.

Hello @Sandeep Rajput 

 

Thank you so much for replying.

 

I changed my scipt like below:

 updateJSON: function(addCat, addSubCat, addService, addOffering) {


        var jsonObject = new GlideRecord('sys_properties');
        jsonObject.get('858b9c2847c6ed502bb635a2e36d432b');
        var jsonValue = jsonObject.getValue('value');

        var js1 = JSON.parse(jsonValue);

        var innerJSON = {};
        innerJSON["service"] = addService;
        innerJSON["offering"] = addOffering;
        js1[addCat] = {addSubCat: innerJSON};

        var js3 = JSON.stringify(js1);
        jsonObject.value = js3;

        jsonObject.update();


    },

 

When I tried to with above inputs in variables. the object is updating incorrectly.

Please see below:

"servicenow": {
        "addSubCat": {
            "service": "c01924e51bb4b1104e645421604bcb21",
            "offering": "f04928e51bb4b1104e645421604bcb36"
        }
    }
 
Instaed of addSubcat --> devloper has to come right?

Can you please help me on this? Thanks a lot.

@Naga Ravindra R Update your code as follows.

 

updateJSON: function(addCat, addSubCat, addService, addOffering) {


        var jsonObject = new GlideRecord('sys_properties');
        jsonObject.get('858b9c2847c6ed502bb635a2e36d432b');
        var jsonValue = jsonObject.getValue('value');

        var js1 = JSON.parse(jsonValue);

        var innerJSON = {};
        innerJSON["service"] = addService;
        innerJSON["offering"] = addOffering;
        var someObj = {};
        someObj[addSubCat] = innerJSON;
        js1[addCat] = someObj;
        var js3 = JSON.stringify(js1);
        jsonObject.value = js3;
        jsonObject.update();
    },

 

Please don't forget to mark my answer correct and helpful.