- 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 06:16 AM
Hi @Vineela1 ,
There is a spelling mistake in how you are using it. You are using capital 'O' in your variable name. Make it small and it should work
jsonobject.jobid
I Hope this helps.
Please mark this helpful if this helps and Accept the solution if this solves your issue.
Regards,
Kamlesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 06:20 AM
Hi Kamlesh,
Please suggest inputs.
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);
variable record has this value:
{
"budget":"value",
"jobid":"value",
"location":"value",
"plbs":"value"
};
Regards,
Vineela
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 06:30 AM - edited 10-28-2022 06:33 AM
Hi @Vineela1 ,
I understand your issue now. The problem is that you have stringified a value that is already a string. I would suggest you try this :
var json_custom_string = gr.getValue("u_custom_field1");
json_custom_string = json_custom_string .replace(";","") ; //Use this line only when data you are retriving is haveing semi-colon ';' at the end
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);
Also, please not if your data is in the below format where I can see extra ';' at the end you will have to remove it first from your string and then try above script. I am sure it will work.
Let me know if still do not works.
I Hope this helps.
Please mark this helpful if this helps and Accept the solution if this solves your issue.
Regards,
Kamlesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 07:49 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2022 07:55 AM - edited 10-28-2022 08:00 AM
Did you include this line in your script highlighed below, I am sure you might have missed this line. Error that you are showing is because if ';' that is at the end of your retreived value:
Thanks,
Kamlesh