How to convert String value to Object

shaik_irfan
Tera Guru

Hi,

 

I have String value which i want to convert as Object.

 

string =  "user firstname" : "first name", "user lastname" :"last name",  "user fullname" :"full name"

 

I want to convert this string into an object. Challenge is we may lot many values in String field which we are not sure so how to set them into 1 object dynamically

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shaik,

that seems to me like a json object;

store that in json rather than string

var jsonObj = {"user firstname":"first name","user lastname":"last name","user fullname":"full name"};

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Ct111
Tera Sage

check this out

http://www.john-james-andersen.com/blog/service-now/converting-a-json-string-to-an-object-in-service...

 

Hope this helps.

 

Mark my ANSWER as CORRECT n HELPFUL if it helped.

Allen Andreas
Administrator
Administrator

Hi,

You would use JSON parse, like so:

var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');

This converts the text in to an object, which can then be used like...

document.getElementById("demo").innerHTML = obj.name + ", " + obj.age; 

See link for more info: https://www.w3schools.com/js/js_json_parse.asp

Please mark reply as Helpful/Correct. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Haha, ok...

Glad you got your answer.

Take care


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shaik,

that seems to me like a json object;

store that in json rather than string

var jsonObj = {"user firstname":"first name","user lastname":"last name","user fullname":"full name"};

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader