SNOW Flow: How do we deal with JSON Stringify for nested objects?

Kasper Nielsen
Kilo Explorer

dictWithNests = {firstObject:itsValue, {nestedObject1:nestedValue1, nestedObject2:nestedValue2}}

I need to JSON.Stringify a collection similar to the above however I keep getting the error: [object object]

Any tips on how to fix this?

I know that function doesnt support nested objects but I just cant believe that there isnt a solution to this, seems like something that should be a really common usecase.

4 REPLIES 4

Anshu_Anand_
Kilo Sage
Kilo Sage

Its does not seems correct JSON payload .

find_real_file.png

You can check here https://jsoneditoronline.org/#left=local.tokago

 

Use JSON.parse() to parse the object and fetch the values.

var parsed = JSON.parse(obj);

var firstobject = obj.firstObject; // itsValue

 

 

 

Regards,
Anshu

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Kasper,

It is possible to nest objects.

Following is a sample Script Include. Following works OK. Will need to see the actual script that being executed to find the problem.

The problem seems to be .toString() somewhere that's converting an object to a string named "object" somewhere in the code.

function getUserInfo(user_id) {
    var result = {};
    result['name'] = this.getUserName(user_id);
    result['role'] = this.getRoleInfo(user_id);
    return JSON.stringify(result);
}

function getUserName(user_id) {
    var grUser = new GlideRecord('sys_user');
    if (grUser.get(user_id)) {
        return grUser.user_name.toString();
    }
    return;
}

function getRoleInfo(user_id) {
        var grUserRole = new GlideRecord('sys_user_has_role');
        grUserRole.addQuery('user', user_id);
        grUserRole.query();
        var result = [];
        while(grUserRole.next()) {
            var roleInfo = {};
            roleInfo['role'] = grUserRole.role.name.toString();
            roleInfo['state'] = grUserRole.state.toString();
            result.push(roleInfo);
        }
        return result;
}

var result = getUserInfo('0a826bf03710200044e0bfc8bcbe5d8b');
gs.info(result);

Execution result:

*** Script: {"name":"adela.cervantsz","role":[{"role":"sn_publications_recipients_user","state":"active"},{"role":"image_admin","state":"active"},{"role":"sn_publications.author","state":"active"}]}

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Kasper,

Fixed the json in the question and wrote a script to stringify it.

var dictWithNests = {"firstObject":"itsValue", "nestedObject": {"nestedObject1":"nestedValue1", "nestedObject2":"nestedValue2"}};
gs.info(dictWithNests );
var jsonStr = JSON.stringify(dictWithNests);
gs.info(jsonStr);

Execution result. The first line is [object Object] because it's a JavaScript object. The second line is the result of stringify().

*** Script: [object Object]
*** Script: {"firstObject":"itsValue","nestedObject":{"nestedObject1":"nestedValue1","nestedObject2":"nestedValue2"}}

I've inserted "nestedObject" because JSON requires a name-value pair and surrounded strings with double quotes because that's JSON requirement.