- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 04:32 AM
Hi Team,
Please suggest inputs, I have javascript variable which I made stringify() to convert to javascript string and made JSON.parse() . When I am trying to fetch the key value it is showing undefined message but that value exist in JSON object.
example:
obj = {
"budget": "value",
"cost":"value"
}
when I am trying to access budget value. It shows me undefined . Please suggest on this.
Thanks and Regards,
Vineela
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 08:05 AM
I tried replicating same in my instance and it's a working script unless format of data structure do not changes. Below the screen shots :
If this still do not works please share the screen shot of field and value it's storing. Like the one of description I have shown. Most probably then the issue is with the data stored.
Regards,
Kamlesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 04:41 AM
hi @Vineela1
Can you try below:
var obj = {
"budget": "value",
"cost":"value"
};
var objStr=JSON.stringify(obj);
var objPar=JSON.parse(objStr);
gs.info(objPar.budget + "\n" + objPar.cost);
Hope it helps..
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 05:14 AM
Hi Murthy,
Thanks for the update. But it is still showing as undefined .when trying to check length , it shows as undefined. But when I am trying to print the javascript JSON object , it prints all values correctly.
jsonobject{
"budget":"value",
"jobid":"value",
"location":"value",
"plbs":"value"
}
jsonObject[jobid]undefined
jsonObject.[jobid]undefined
like If I try to print : "jsonObject.jobid" then output: undefined
jsonObject[jobid] - output : jsonObject[jobid] undefined
Regards,
Vineela
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 05:59 AM
Hi @Vineela1,
Try this updated scripts or scripts shared by @Murthy Ch. It is correct and working as expected.
var jsonobject = {
"budget": "value",
"jobid": "value",
"location": "value",
"plbs": "value"
};
var test = JSON.stringify(jsonobject);
var parsed = JSON.parse(test);
gs.info("budget: " + parsed.budget);
gs.info("jobid: " + parsed.jobid);
gs.info("location: " + parsed.budget);
gs.info("plbs: " + parsed.budget);
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 06:17 AM
Hi Sagar,
same issue I am getting same output : "undefined" for location , jobid and plbs values . Actually I am trying to copy of one variable to another table whose JSON format.
code:
var json_custom_string = JSON.stringify(gr.u_custom_field1.getValue());
gs.log("json string:"+json_custom_string);
var len = json_custom_string.length;
gs.log('length is : ' + len);
var jsonObject = JSON.parse(json_custom_string);
gs.log("jsonobject: "+jsonObject);
gs.info("jsonObject.jobid :"+jsonObject.jobid);
gs.info("jsonObject.location :"+jsonObject.location);
gs.info("jsonObject.plbs :"+jsonObject.plbs);
Regards,
Vineela