Unable to fetch the key value for JSON parsed in Javascript getting undefined message

Vineela1
Tera Contributor

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

1 ACCEPTED SOLUTION

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 :

kamleshkjmar_2-1666969437777.png

 

 

kamleshkjmar_1-1666969405472.png

 

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 

View solution in original post

15 REPLIES 15

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

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

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

 

Hi Kamlesh,

 

Tried this code but getting new error Unable to understand. Please help. Earlier I have already tried to pass directly without stringify. But it is empty and null showing. Then only I have made stringify step.

 

Regards,

Vineela

@Vineela1 ,

 

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:

kamleshkjmar_0-1666968900826.png

 

Thanks,

Kamlesh