Store values in JSON format

SnowUserDev
Tera Contributor

Hey Community,

 

How can we Store values in JSON format in servicenow in system properties. I want to store multiple values in a single property.

 

Thanks

3 ACCEPTED SOLUTIONS

Danish Bhairag2
Tera Sage
Tera Sage

Hi @SnowUserDev ,

 

U can do something like this.

 

{

"key1":"value1",

"key2":"value2",

"key3":"value3"

}

 

Thanks,

Danish

 

View solution in original post

Hi @SnowUserDev ,

 

U can create a property of type string n then put the values in the above format.

 

Thanks,

Danish

 

View solution in original post

Stefan Georgiev
Tera Guru

Hello @SnowUserDev ,

if you share your case, we might find another solution than storing in a property.
you can store it in a string property, just go to Properties and store you JSON in a string property type record.

When you are getting the property anfte you use gs.getProperty('<your_JSON_property') use an JSON.parse

var prop = gs.getProperty('<your_JSON_property')
va yourObject = JSON.parse(prop)

 

/*you logic here with the Object*/

Hope that this helped, if it does you can mark it as helpful and accept the solution.

All the best, 
Stefan

View solution in original post

9 REPLIES 9

Hi @SnowUserDev ,

 

You can try the way which @Stefan Georgiev has suggested just adding 1 more step to it.

 

If u want to use 1 of the key & its value u can write something like this

 

var a = yourObject.key1; //this will print the value present in it.

 

Thanks,

Danish

 

Stefan Georgiev
Tera Guru

Hello @SnowUserDev ,

if you share your case, we might find another solution than storing in a property.
you can store it in a string property, just go to Properties and store you JSON in a string property type record.

When you are getting the property anfte you use gs.getProperty('<your_JSON_property') use an JSON.parse

var prop = gs.getProperty('<your_JSON_property')
va yourObject = JSON.parse(prop)

 

/*you logic here with the Object*/

Hope that this helped, if it does you can mark it as helpful and accept the solution.

All the best, 
Stefan

Hi @Stefan Georgiev & @Danish Bhairag2 

 

Thanks for ur responses., 😊

I've tried this and give me this error 
com.glide.RhinoEcmaError Unexpected token:0

Muhammad Hamza
Kilo Sage

Hey @SnowUserDev,

I assume that you want this feature for your custom application which will you use. You can create a choice option JSON for Type in the System Property [sys_properties] table. As it is not there out-of-the-box.

MuhammadHamza_0-1702327177897.png

You can use it in your properties. Make sure you store the correct formatted JSON value in the properties.

MuhammadHamza_1-1702327373047.png

Kindly appreciate the efforts of community contributors by marking appropriate response as the correct solution and helpful, this may help other community users to follow the right solution in the future.

Thanks,

Hamza