Pass variable value to json object

Elena10
Tera Contributor

Hi,

I am trying to pass a variable into a json but unfortunatly the output is empty.

If I take this exemple

// Creating the object to later be JSON encoded var json = {"opened_by":"62826bf03710200044e0bfc8bcbe5df1","requested_for": newReferent,"department":"221f3db5c6112284009f4becd3039cc9"};

the line requested_for of the object return empty {}

But when I log it just before it return the sys id of the user.

Can someone know how to pass variable to json  ?

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

Hi Elena,

I am going to make some assumptions since the code for getting newReferent isn't provided. But when you are assigning newReferent your post reads like it should be a sys_id. I'm assuming you're getting that from a GlideRecord or maybe even a value from a field.

When assigning to newReferent I would make it a string. Or at least when assigning it to your literal object, assign as a  string.

i.e.

var json = {
    "opened_by":"62826bf03710200044e0bfc8bcbe5df1",
    "requested_for": newReferent.toString(),
    "department":"221f3db5c6112284009f4becd3039cc9"
};

 

Depending upon how and where you are assigning newReferent the type is getting skewed.

View solution in original post

2 REPLIES 2

Mohith Devatte
Tera Sage
Tera Sage

hello @Elena ,

usually we pass elements into json like below 

var obj ={};

obj.name ="mohith";

obj.company="globant";

and if you try to print obj its {"name":"mohith",""company":"globant"}

But in your case paste the script how you are trying to build the json then we will be able to guide you 

Please mark my answer correct if it helps you

ChrisBurks
Mega Sage

Hi Elena,

I am going to make some assumptions since the code for getting newReferent isn't provided. But when you are assigning newReferent your post reads like it should be a sys_id. I'm assuming you're getting that from a GlideRecord or maybe even a value from a field.

When assigning to newReferent I would make it a string. Or at least when assigning it to your literal object, assign as a  string.

i.e.

var json = {
    "opened_by":"62826bf03710200044e0bfc8bcbe5df1",
    "requested_for": newReferent.toString(),
    "department":"221f3db5c6112284009f4becd3039cc9"
};

 

Depending upon how and where you are assigning newReferent the type is getting skewed.