Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Database Column Type "JSON" is not available for selection on a custom field

stevemac
Tera Guru

Have a requirement to store some structured data in a field. Was thinking XML or JSON (with preference for JSON). I notice that ServiceNow has a a field type of JSON, but it is hidden from selection and the record is read only. So have the following questions

- has anyone used the JSON field type on custom fields? If yes -
- how did you create the column record with the correct type (e.g modified XML and imported it, used script to unhide the JSON type, etc)?
- any ramifications?

- If the JSON field type is unavailable - I assume a String field is the way to go.
- how do you manage JSON format validation?

1 ACCEPTED SOLUTION

Sample code.

var jsonObj = {"users":[{"name": "ozawa","user_id":"ozawa_h"}]};

// convert json object to string
var jsonStr = JSON.stringify(jsonObj);
gs.info(jsonStr);

// convert string to json object
var jsonObj2 = JSON.parse(jsonStr);
var users = jsonObj2.users;
for (var i=0; i<users.length; i++) {
  gs.info(users[i].name);
} 

View solution in original post

3 REPLIES 3

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Steve,

I usually just JSON.stringify() the object and save it in a string field.

Sample code.

var jsonObj = {"users":[{"name": "ozawa","user_id":"ozawa_h"}]};

// convert json object to string
var jsonStr = JSON.stringify(jsonObj);
gs.info(jsonStr);

// convert string to json object
var jsonObj2 = JSON.parse(jsonStr);
var users = jsonObj2.users;
for (var i=0; i<users.length; i++) {
  gs.info(users[i].name);
}