var key = " ";
var myBio = {
"name":"Sathya",
"age":"23",
"position":"Soft.Engineer",
"email":
{
"email1":"[email protected]",
"email2":"[email protected]"},
};
for (key in myBio){
var y = myBio[key];
console.log(key+" : "+ y);
}
output:
- name : Sathya
- age : 23
- position : Soft.Engineer
- email : [object Object]
Required Output:
- name : Sathya
- age : 23
- position : Soft.Engineer
- email :
- email1:[email protected]
- email2:[email protected]
I can print this Emails separately using another loop. But I want to print with main loop only . Any ways to do this using JavaScript??
var key = " ";
var myBio = {
"name":"Sathya",
"age":"23",
"position":"Soft.Engineer",
"email":
{
"email1":"[email protected]",
"email2":"[email protected]"},
};
for (key in myBio){
var y = myBio[key];
console.log(key+" : "+ y);
}
output:
- name : Sathya
- age : 23
- position : Soft.Engineer
- email : [object Object]
Required Output:
- name : Sathya
- age : 23
- position : Soft.Engineer
- email :
- email1:[email protected]
- email2:[email protected]
I can print this Emails separately using another loop. But I want to print with main loop only . Any ways to do this using JavaScript??
Share Improve this question edited Jan 11, 2017 at 9:55 HaBo 14.3k39 gold badges118 silver badges214 bronze badges asked Jan 11, 2017 at 9:47 Sathyanarayana K USathyanarayana K U 111 silver badge3 bronze badges 2-
It's because you try to concatentate object to string. You don't have to do that. Solution is to use
console.log(key, y)
– Piotr Lewandowski Commented Jan 11, 2017 at 9:53 - did you try simple console.log(myBio)? – Ayyash Commented Jan 11, 2017 at 10:00
5 Answers
Reset to default 5Most people searching in Google and finding this question are probably looking for something like this:
console.log(JSON.stringify(object, null, 2));
This prints deeply nested JSON to the console without the annoying "[Object]".
The last two parameters are optional. The 2 says to pretty print the JSON with an indent of 2. If left blank, will output non-formatted JSON.
This does not answer this specific question, but hopefully will answer those searching for "Print nested JSON Javascript".
You could use a recursive function to get this done.. example
function logRecursive(object){
for (key in object){
var value=object[key];
if(typeof value === 'object'){
console.log('{');
logRecursive(value)
console.log('}');
}else{
console.log(value);
}
}
function recursion(myBio) {
for (var key in myBio) {
if (typeof(myBio[key]) == 'object') {
recursion(myBio[key]);
} else {
alert("Key: " + key + " Values: " + myBio[key]);
}
}
}
use this subroutine if you have nested json
Try this:
var myBio = {
"name" : "Sathya",
"age" : "23",
"position" : "Soft.Engineer",
"email" : {
"email1" : "[email protected]",
"email2" : "[email protected]"
},
};
function print(bio) {
for (var key in bio) {
if (typeof(bio[key]) == 'object') {
print(bio[key]);
} else {
console.log(key + ": " + bio[key]);
}
}
}
print(myBio);
Try this :
var myBio = {
"name":"Sathya",
"age":"23",
"position":"Soft.Engineer",
"email": {
"email1":"[email protected]",
"email2":"[email protected]"
}
};
var result = parseJSON(myBio);
console.log(result);
function parseJSON(obj) {
var parsedData = '';
for(var i in obj) {
if (typeof obj[i] == 'object') {
parsedData += parseJSON(obj[i]);
}else {
parsedData += i +' : '+ obj[i];
}//end if
parsedData += '\n';
}//end for
return parsedData;
}//end function