Saura Sambit
Tera Expert

JSONs are widely recognised for their capability to store multiple values in a single variable. In ServiceNow, we frequently use JSONs in our scripts; however, there is also a dictionary data type available. This allows you to store a simplified version of JSON within a record in a table, organised as Name-Value pairs.

 

When you need to store data in a key-value pair format, rather than creating a separate many-to-many (m2m) table with just two columns, the ‘Name-Value Pairs‘ dictionary type can be a more efficient solution. A common use case for this could be storing rate cards within a task record.

 

Dictionary definition

 

name_value_pair_dictionary_record.png

 

Form view

 

name_value_pair_form_view.png

 

Script usage

 

  • GlideRecord.getValue(‘u_rate_cards’) will return a string JSON like
{"Engineer":"100","Executive":"60","Analyst":"50"}
  • GlideRecord.u_rate_cards.Engineer will return ‘100’
  • GlideRecord.u_rate_cards.Analyst will return ’50’
  • and so on…

 

The field value can be set in a similar manner.

nv = '{"Engineer":"100","Executive":"60","Analyst":"50"}';
GlideRecord.u_rate_cards = nv;


OR


GlideRecord.u_rate_cards.Engineer = "100";
GlideRecord.u_rate_cards.Executive = "60";
GlideRecord.u_rate_cards.Analyst = "50";

 

Next steps

 

Familiarise yourself with this dictionary type and keep it in mind, as it could prove useful in the future when the need arises in your process.

Comments
Mike116
Tera Contributor

Thanks Saura, Excellent write-up!

James Fricker
Tera Guru

How does this compare with dynamic schema fields?

Version history
Last update:
‎09-18-2024 09:30 PM
Updated by:
Contributors