
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2022 03:10 AM
On doing a GlideAjax I am getting a String written in JSON format.
var str ="var data = {"value1": "abc","value2": "def","value3": "ghi"};";
Now from this string I want to fetch individual parameters from this data variable and get output of individual.
Value1 = abc
Value2 = def
Value3 = ghi
Please let me know how from the output string which has a variable declaration of var data, I can get my individual output.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2022 04:17 AM
var str='var data = {"value":"abc","value2":"def","value3":"ghi"};';
var start=str.indexOf('{');
var end=str.indexOf('}');
var str=str.substring(start,end+1)
gs.info(str)
var data=JSON.parse(str); // Now you will have a valid JSON String
//Now access them as below
gs.info(data.value);// Now data.value1 will hold "abc";
gs.info(data.value2);// Now data.value2 will hold "def";
gs.info(data.value3);// Now data.value2 will hold "ghi";
Its my bad i additionaly added the parsing statement twice;
please try the above cod. its tested

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2022 03:21 AM
data = JSON.parse(data);//Will convert the string in to JSON
//Now access them as below
gs.info(data.value1);// Now data.value1 will hold "abc";
gs.info(data.value2);// Now data.value2 will hold "def";
gs.info(data.value3);// Now data.value2 will hold "ghi";
Please mark it as helpful/correct If my answer is helpful in any way,
Best regards,
Thameem

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2022 03:45 AM
Below is the string which I want to convert:
var str ="var data = {"value1": "abc","value2": "def","value3": "ghi"};";

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2022 03:53 AM
first of alll. I dont think what you have is a valid string.
i think what the str must of contain is
var str='var data = {"value1": "abc","value2": "def","value3": "ghi"}';
If the string contains double quotation(") then the string should use single quotation(') to wrap it
now if its the case use the below code
var str='var data = {"value1": "abc","value2": "def","value3": "ghi"}';
var start=str.indexOf('{');
var end=str.indexOf('}');
var data=JSON.parse(str.substring(start,end+1)); // Now you will have a valid JSON String
data = JSON.parse(data);// now Will convert the JSON string in to JSON
//Now access them as below
gs.info(data.value1);// Now data.value1 will hold "abc";
gs.info(data.value2);// Now data.value2 will hold "def";
gs.info(data.value3);// Now data.value2 will hold "ghi";

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2022 04:13 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2022 04:17 AM
var str='var data = {"value":"abc","value2":"def","value3":"ghi"};';
var start=str.indexOf('{');
var end=str.indexOf('}');
var str=str.substring(start,end+1)
gs.info(str)
var data=JSON.parse(str); // Now you will have a valid JSON String
//Now access them as below
gs.info(data.value);// Now data.value1 will hold "abc";
gs.info(data.value2);// Now data.value2 will hold "def";
gs.info(data.value3);// Now data.value2 will hold "ghi";
Its my bad i additionaly added the parsing statement twice;
please try the above cod. its tested