Beginner JavaScript, JSON indexing question: why is my (for … in) script failing?












0














I am not a seasoned Javascript coder or even too familiar with JSON. My approach is likely very naive. Recommendations for better approaches are welcome.



Here's my code.



<!DOCTYPE html>
<html>
<head>
<script>
database = {
"name": "AdventureWorks2012 vs AdventureWorksModified",
"children": [{
"name": "Person",
"children": [{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
},{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
}]
},{
"name": "PersonDELETEmePLEASE",
"children": [{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
}]
}]
}

console.log(`name : ${database.name}`);
console.log("=SCHEMAS=");
for (var i in database.children){
console.log(`name : ${database.children[i].name}`);
console.log("children: (-TABLES-)");

for (var j in database.children){
console.log(`name : ${database.children[i].children[j].name}`);
console.log("children: (-COLUMNS-)");

for (var k in database.children[i].children[j].children){
console.log(`name : ${database.children[i].children[j].children[k].name}`);
console.log("children: (-DATA-)");

for (var l in database.children[i].children[j].children[k].attributes){
console.log(`${l} : ${database.children[i].children[j].children[k].attributes[l]}`);
}
}
}
console.log("nnn");
}
</script>
</head>
<body>
</body>
</html>


The structure is database -> schemas -> tables -> columns + column data.



AdventureWorks2012 vs AdventureWorksModified -> Person -> Address -> AddressID + attributes.



As mentioned in the title, I am trying to iterate through some JSON data. Whether if outputted to HTML or to the console, I run into an error on a strange condition: whenever a schema has just ONE table. It can have one child, or one data member, but it MUST have more than one table.



This is the error I get:



This is the error I get.



Please aid my understanding: what's in my looping causes this to happen? I want to ask before I start looking for workarounds and end up leading myself into a ditch.



Thanks for any help.










share|improve this question
























  • In your second for loop (for (var j in database.children)) i believe it should read for (var j in database.children[i].children)
    – dotconnor
    Nov 19 '18 at 18:45






  • 1




    FYI: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. What you have there is just an object.
    – T.J. Crowder
    Nov 19 '18 at 18:46








  • 1




    That is not JSON, it is a JavaScript object literal. JSON is always a string.
    – Amy
    Nov 19 '18 at 18:46










  • Okay, thanks for the clarifications. I'll keep these in mind.
    – Werewoof
    Nov 19 '18 at 19:26
















0














I am not a seasoned Javascript coder or even too familiar with JSON. My approach is likely very naive. Recommendations for better approaches are welcome.



Here's my code.



<!DOCTYPE html>
<html>
<head>
<script>
database = {
"name": "AdventureWorks2012 vs AdventureWorksModified",
"children": [{
"name": "Person",
"children": [{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
},{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
}]
},{
"name": "PersonDELETEmePLEASE",
"children": [{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
}]
}]
}

console.log(`name : ${database.name}`);
console.log("=SCHEMAS=");
for (var i in database.children){
console.log(`name : ${database.children[i].name}`);
console.log("children: (-TABLES-)");

for (var j in database.children){
console.log(`name : ${database.children[i].children[j].name}`);
console.log("children: (-COLUMNS-)");

for (var k in database.children[i].children[j].children){
console.log(`name : ${database.children[i].children[j].children[k].name}`);
console.log("children: (-DATA-)");

for (var l in database.children[i].children[j].children[k].attributes){
console.log(`${l} : ${database.children[i].children[j].children[k].attributes[l]}`);
}
}
}
console.log("nnn");
}
</script>
</head>
<body>
</body>
</html>


The structure is database -> schemas -> tables -> columns + column data.



AdventureWorks2012 vs AdventureWorksModified -> Person -> Address -> AddressID + attributes.



As mentioned in the title, I am trying to iterate through some JSON data. Whether if outputted to HTML or to the console, I run into an error on a strange condition: whenever a schema has just ONE table. It can have one child, or one data member, but it MUST have more than one table.



This is the error I get:



This is the error I get.



Please aid my understanding: what's in my looping causes this to happen? I want to ask before I start looking for workarounds and end up leading myself into a ditch.



Thanks for any help.










share|improve this question
























  • In your second for loop (for (var j in database.children)) i believe it should read for (var j in database.children[i].children)
    – dotconnor
    Nov 19 '18 at 18:45






  • 1




    FYI: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. What you have there is just an object.
    – T.J. Crowder
    Nov 19 '18 at 18:46








  • 1




    That is not JSON, it is a JavaScript object literal. JSON is always a string.
    – Amy
    Nov 19 '18 at 18:46










  • Okay, thanks for the clarifications. I'll keep these in mind.
    – Werewoof
    Nov 19 '18 at 19:26














0












0








0







I am not a seasoned Javascript coder or even too familiar with JSON. My approach is likely very naive. Recommendations for better approaches are welcome.



Here's my code.



<!DOCTYPE html>
<html>
<head>
<script>
database = {
"name": "AdventureWorks2012 vs AdventureWorksModified",
"children": [{
"name": "Person",
"children": [{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
},{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
}]
},{
"name": "PersonDELETEmePLEASE",
"children": [{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
}]
}]
}

console.log(`name : ${database.name}`);
console.log("=SCHEMAS=");
for (var i in database.children){
console.log(`name : ${database.children[i].name}`);
console.log("children: (-TABLES-)");

for (var j in database.children){
console.log(`name : ${database.children[i].children[j].name}`);
console.log("children: (-COLUMNS-)");

for (var k in database.children[i].children[j].children){
console.log(`name : ${database.children[i].children[j].children[k].name}`);
console.log("children: (-DATA-)");

for (var l in database.children[i].children[j].children[k].attributes){
console.log(`${l} : ${database.children[i].children[j].children[k].attributes[l]}`);
}
}
}
console.log("nnn");
}
</script>
</head>
<body>
</body>
</html>


The structure is database -> schemas -> tables -> columns + column data.



AdventureWorks2012 vs AdventureWorksModified -> Person -> Address -> AddressID + attributes.



As mentioned in the title, I am trying to iterate through some JSON data. Whether if outputted to HTML or to the console, I run into an error on a strange condition: whenever a schema has just ONE table. It can have one child, or one data member, but it MUST have more than one table.



This is the error I get:



This is the error I get.



Please aid my understanding: what's in my looping causes this to happen? I want to ask before I start looking for workarounds and end up leading myself into a ditch.



Thanks for any help.










share|improve this question















I am not a seasoned Javascript coder or even too familiar with JSON. My approach is likely very naive. Recommendations for better approaches are welcome.



Here's my code.



<!DOCTYPE html>
<html>
<head>
<script>
database = {
"name": "AdventureWorks2012 vs AdventureWorksModified",
"children": [{
"name": "Person",
"children": [{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
},{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
}]
},{
"name": "PersonDELETEmePLEASE",
"children": [{
"name": "Address",
"children": [{
"name": "AddressID",
"attributes": {
"coltype": "int",
"coldefault": null,
"colordinal": 1,
"colCharLength": null
}
}]
}]
}]
}

console.log(`name : ${database.name}`);
console.log("=SCHEMAS=");
for (var i in database.children){
console.log(`name : ${database.children[i].name}`);
console.log("children: (-TABLES-)");

for (var j in database.children){
console.log(`name : ${database.children[i].children[j].name}`);
console.log("children: (-COLUMNS-)");

for (var k in database.children[i].children[j].children){
console.log(`name : ${database.children[i].children[j].children[k].name}`);
console.log("children: (-DATA-)");

for (var l in database.children[i].children[j].children[k].attributes){
console.log(`${l} : ${database.children[i].children[j].children[k].attributes[l]}`);
}
}
}
console.log("nnn");
}
</script>
</head>
<body>
</body>
</html>


The structure is database -> schemas -> tables -> columns + column data.



AdventureWorks2012 vs AdventureWorksModified -> Person -> Address -> AddressID + attributes.



As mentioned in the title, I am trying to iterate through some JSON data. Whether if outputted to HTML or to the console, I run into an error on a strange condition: whenever a schema has just ONE table. It can have one child, or one data member, but it MUST have more than one table.



This is the error I get:



This is the error I get.



Please aid my understanding: what's in my looping causes this to happen? I want to ask before I start looking for workarounds and end up leading myself into a ditch.



Thanks for any help.







javascript json for-loop indexing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:54









kit

1,1063716




1,1063716










asked Nov 19 '18 at 18:39









WerewoofWerewoof

74




74












  • In your second for loop (for (var j in database.children)) i believe it should read for (var j in database.children[i].children)
    – dotconnor
    Nov 19 '18 at 18:45






  • 1




    FYI: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. What you have there is just an object.
    – T.J. Crowder
    Nov 19 '18 at 18:46








  • 1




    That is not JSON, it is a JavaScript object literal. JSON is always a string.
    – Amy
    Nov 19 '18 at 18:46










  • Okay, thanks for the clarifications. I'll keep these in mind.
    – Werewoof
    Nov 19 '18 at 19:26


















  • In your second for loop (for (var j in database.children)) i believe it should read for (var j in database.children[i].children)
    – dotconnor
    Nov 19 '18 at 18:45






  • 1




    FYI: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. What you have there is just an object.
    – T.J. Crowder
    Nov 19 '18 at 18:46








  • 1




    That is not JSON, it is a JavaScript object literal. JSON is always a string.
    – Amy
    Nov 19 '18 at 18:46










  • Okay, thanks for the clarifications. I'll keep these in mind.
    – Werewoof
    Nov 19 '18 at 19:26
















In your second for loop (for (var j in database.children)) i believe it should read for (var j in database.children[i].children)
– dotconnor
Nov 19 '18 at 18:45




In your second for loop (for (var j in database.children)) i believe it should read for (var j in database.children[i].children)
– dotconnor
Nov 19 '18 at 18:45




1




1




FYI: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. What you have there is just an object.
– T.J. Crowder
Nov 19 '18 at 18:46






FYI: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. What you have there is just an object.
– T.J. Crowder
Nov 19 '18 at 18:46






1




1




That is not JSON, it is a JavaScript object literal. JSON is always a string.
– Amy
Nov 19 '18 at 18:46




That is not JSON, it is a JavaScript object literal. JSON is always a string.
– Amy
Nov 19 '18 at 18:46












Okay, thanks for the clarifications. I'll keep these in mind.
– Werewoof
Nov 19 '18 at 19:26




Okay, thanks for the clarifications. I'll keep these in mind.
– Werewoof
Nov 19 '18 at 19:26












1 Answer
1






active

oldest

votes


















3














Your two loops loop over the same thing



for (var i in database.children) { <-- same
for (var j in database.children) <-- same


You forgot to reference the nesting



for (var i in database.children) { <-- first level
for (var j in database.children[i].children) <-- second level
console.log(database.children[i].children[j])





share|improve this answer

















  • 1




    Oh wow, I am a dense oatmeal cookie. Thank you a ton good sir.
    – Werewoof
    Nov 19 '18 at 18:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380777%2fbeginner-javascript-json-indexing-question-why-is-my-for-in-script-faili%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Your two loops loop over the same thing



for (var i in database.children) { <-- same
for (var j in database.children) <-- same


You forgot to reference the nesting



for (var i in database.children) { <-- first level
for (var j in database.children[i].children) <-- second level
console.log(database.children[i].children[j])





share|improve this answer

















  • 1




    Oh wow, I am a dense oatmeal cookie. Thank you a ton good sir.
    – Werewoof
    Nov 19 '18 at 18:56
















3














Your two loops loop over the same thing



for (var i in database.children) { <-- same
for (var j in database.children) <-- same


You forgot to reference the nesting



for (var i in database.children) { <-- first level
for (var j in database.children[i].children) <-- second level
console.log(database.children[i].children[j])





share|improve this answer

















  • 1




    Oh wow, I am a dense oatmeal cookie. Thank you a ton good sir.
    – Werewoof
    Nov 19 '18 at 18:56














3












3








3






Your two loops loop over the same thing



for (var i in database.children) { <-- same
for (var j in database.children) <-- same


You forgot to reference the nesting



for (var i in database.children) { <-- first level
for (var j in database.children[i].children) <-- second level
console.log(database.children[i].children[j])





share|improve this answer












Your two loops loop over the same thing



for (var i in database.children) { <-- same
for (var j in database.children) <-- same


You forgot to reference the nesting



for (var i in database.children) { <-- first level
for (var j in database.children[i].children) <-- second level
console.log(database.children[i].children[j])






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 18:45









epascarelloepascarello

151k13131179




151k13131179








  • 1




    Oh wow, I am a dense oatmeal cookie. Thank you a ton good sir.
    – Werewoof
    Nov 19 '18 at 18:56














  • 1




    Oh wow, I am a dense oatmeal cookie. Thank you a ton good sir.
    – Werewoof
    Nov 19 '18 at 18:56








1




1




Oh wow, I am a dense oatmeal cookie. Thank you a ton good sir.
– Werewoof
Nov 19 '18 at 18:56




Oh wow, I am a dense oatmeal cookie. Thank you a ton good sir.
– Werewoof
Nov 19 '18 at 18:56


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380777%2fbeginner-javascript-json-indexing-question-why-is-my-for-in-script-faili%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory