- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2025 01:44 AM
Hi there,
I have created system property with key value pair. In script I can return value by passing key but is it possible to return key by passing value?
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2025 04:25 AM
Resolved:
var propertyValue = gs.getProperty('testproperty');
//{"LATAM":["Argentina", "India", "Brazil"]}
var regionMap = JSON.parse(propertyValue);
var targetCountry = 'India';
for (var region in regionMap) {
var countries = regionMap[region];
if (countries.indexOf(targetCountry) !== -1) {
gs.info("country" + targetCountry + "has region " + region);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2025 01:50 AM - edited 06-06-2025 01:51 AM
Hi @Narayan K ,
Please use the system property in JSON key pair values. In that way you can get key based on value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2025 01:54 AM
Hello Kindly see below script which might help you:
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2025 02:10 AM
something like this can help you get started
Ensure you use system property value but I hard-coded the json string
//var prop = gs.getProperty('my_property'); // Replace with your property name
var prop = '{"US":"United States","IN":"India","UK":"United Kingdom"}';
var obj = JSON.parse(prop);
var searchValue = 'India'; // The value you want to find the key for
var foundKey = null;
for (var key in obj) {
if (obj[key] == searchValue) {
foundKey = key;
break;
}
}
gs.info('Key for value "' + searchValue + '" is: ' + foundKey);
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2025 03:20 AM
Hi @Ankur Bawiskar ,
Thanks for the solution. I am able to get Key by using value but currently I am storing multiple values for a single key as shown below:
{"LATAM":["Argentina","India","Brazil"]}
Given code is not working for multiple country values.