0

i used this code in nodejs to get data from json but gets error and say undefined

var obj = {payload:'fp_2'};
var myJSON = JSON.stringify(obj);
console.log(myJSON.payload); //output: undefined

and i have same error in javascript

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>

var obj = {payload:'fp_2'};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON.payload;

</script>

</body>
</html>

what is my error? can any one help me...

3
  • The result of JSON.stringify() is just a simple string; there's no "payload" property. You can examine obj.payload because that makes sense. Once you .stringify() it however you just have a string. There's no need to "parse" an object that's declared with an object initializer expression; the object already exists and is ready for use. Commented Aug 6, 2017 at 14:35
  • Do you realize what JSON.stringify(obj) does? It stringifies the object. It returns a string. If you only need the payload property, you don't need to stringify. Commented Aug 6, 2017 at 14:37
  • yes i know , i using nodejs i have used obj.payload but not working and gets this error "TypeError: Cannot read property 'payload' of undefined at receivedMessage (/home/ubuntu/workspace/index.js:535:20)" Commented Aug 6, 2017 at 14:42

3 Answers 3

1

obj is already object, you can

var obj = {payload:'fp_2'};
console.log(myJSON.payload);

to parse string to object use JSON.parse

var str = '{"payload": "fp_2"}';
var myJSON = JSON.parse(str);
console.log(myJSON.payload);

JSON.stringify used to convert obejct to string

var obj = {payload:'fp_2'};
var str = JSON.stringify(obj);
console.log(str);
// {"payload":"fp_2"}
Sign up to request clarification or add additional context in comments.

1 Comment

i using nodejs i have used obj.payload but not working and gets this error "TypeError: Cannot read property 'payload' of undefined at receivedMessage (/home/ubuntu/workspace/index.js:535:20)"
0

What happens in both your example is you are trying to get property "payload" from a string, which does not have this kind of property. The method JSON.stringify() creates string from any given object, so your custom object with property "payload" was transformed to JS string object, which does not have it anymore.

This is the list of properties, that JS string has:

  • constructor
  • length
  • prototype

You can find definition of each property and read more about strings in JS with this link: W3 Schools JS string

If you want to use "payload" from your custom object, you don't need to JSON.stringify() it. Just use it:

var obj = {payload:'fp_2'};
console.log(obj.payload);

// Or in your html example
var obj = {payload:'fp_2'};
document.getElementById("demo").innerHTML = obj.payload;

2 Comments

yes i know , i using nodejs i have used obj.payload but not working and gets this error "TypeError: Cannot read property 'payload' of undefined at receivedMessage (/home/ubuntu/workspace/index.js:535:20)"
@PawanOsman can you give example of code, that gives you TypeError obj as undefined?
0

write this for nodejs

var obj = {payload:'fp_2'};
console.log(obj.payload);

write this for script file

<script>

var obj = {payload:'fp_2'};
document.getElementById("demo").innerHTML = obj.payload;

</script>

3 Comments

i using nodejs i have used obj.payload but not working and gets this error "TypeError: Cannot read property 'payload' of undefined at receivedMessage (/home/ubuntu/workspace/index.js:535:20)"
This error occurs when you have defined variable, but not give value to it and you are trying to access value.
but i know that payload variable has a value and equal to "fp_2" i want tt get it but get this error . my json data is from request. i have messenger bot i want to detect quick reply payload . i get that QrPayload variable equal to "{payload:'fp_2'}" but when i use QrPayload.payload get that error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.