undefined when trying access data from JSON string - javascript

How can I access a specific data from following JSON without getting undefined?
var myObj = '{"isTrue":"true","id":"1"}';
var theKey = 'isTrue';
alert(myObj[theKey]); //I get undefined here

You need to parse that JSON as String before to access any property from it.
JSON.parse(myObj)
var myObj = '{"isTrue":"true","id":"1"}';
var theKey = 'isTrue';
alert(JSON.parse(myObj)[theKey]);

Related

Access to properties of JSON in Node JS

I'm trying to access the age but I cannot, how can I do that?
var json = '{"carlos":{"data":{"age":"30","phone":"3226458186"}}}';
var obj = JSON.parse(JSON.stringify(json));
This doesn't work for me, obj.carlos is undefined
console.log("Age of Carlos: ", obj.carlos.data.age);
The problem here is the unnecessary call to JSON.stringify, that method is used to convert JavaScript objects to JSON, but has nothing to do with deserializing them.
The code that you need is just this:
var obj = JSON.parse(json);
No need to JSON.stringify. You just only need to parse your values as they are already a JSON string. Here you go:
var json = '{"carlos":{"data":{"age":"30","phone":"3226458186"}}}';
var obj = JSON.parse(json);
console.log("Age: ", obj.carlos.data.age);
the problem here is the unnecessary call to JSON.parse(JSON.stringify(json)) conver javascript object to JSON like: JSON.parse(json)
example :
var json = '{"carlos":{"data":{"age":"30","phone":"3226458186"}}}';
var obj = JSON.parse(JSON.stringify(json));
console.log("Phone of Carlos: ", obj.carlos.data.phone);
You cannot use JSON.stringify() here, because this method converts a JavaScript object or value to a JSON string and you already got a JSON string.
So your code should look like this:
var json = '{"carlos":{"data":{"age":"30","phone":"3226458186"}}}';
var obj = JSON.parse(json);

i want to parse this json data with nodejs but gets error undefined

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...
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"}
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;
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>

Dynamically add object in javascript array

I have json:
var obj = '{"Form":[],"Provider":[]}';
I push the data with variable value to make dynamic objects:
var pName = 'Tester';
var data = {
pName :["testing"]
};
console.log(obj['Provider'].push(data));
But that adds pName as variable name but not variable value that is Tester, i tried with +pName+ that also not works.
Returns:
{"Form":[],"Provider":[{"pName":["Testing"]}]}
Any help would be appreciated.
You must use [] syntax near the property name.It will evaluate the expression in the [] and returns the value.
See the example.Here the data's property is with the name 'Tester'.
var obj = {"Form":[],"Provider":[]};
var pName = 'Tester';
var data = {
[pName] :["testing"]
};
console.log(data.pName); // undefined
console.log(data.Tester); // OK
obj['Provider'].push(data);
console.log(obj);

Node.js - JSON.parse - Add Nodes to the Result

If I parse a JSON object in Node.js like this
var datap = JSON.parse(data);
I get an hierarchical object (as visible in the debugger):
datap = object
account1 = object
name = "A"
type = "1"
account2 = object
name = "B"
type = "2"
Now I would like to add an account3 with a line like this
datap.append("account3")
and ofcourse after this
datap.account3.append({"name":"C", "type":"1"})
My Javascript knowledge is still limited, is there a possibility to do so?
Once the json is parsed you can just use in as an Object:
var datap = JSON.parse(data);
datap.account3 = { name :"C", type :"1"};

Dynamic Javascript Object creation

How can I make php array into Javascript object code?.
You can try this. This is how you dynamically name object properties and object values. I am assuming you want to build this dynamically where the property names are coming from a source. All you have to do is loop through the values
var treeData = {};
Loop Here
var nameProperty = "propertyName";
var nameValue = "propertyValue";
var childProperty = "child";
var childValue = {};
Try This.
http://jsfiddle.net/zKdMa/1/

Categories

Resources