Beginner JavaScript, JSON indexing question: why is my (for … in) script failing?
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:
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
add a comment |
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:
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
In your second for loop (for (var j in database.children)
) i believe it should readfor (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
add a comment |
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:
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
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:
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
javascript json for-loop indexing
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 readfor (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
add a comment |
In your second for loop (for (var j in database.children)
) i believe it should readfor (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
add a comment |
1 Answer
1
active
oldest
votes
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])
1
Oh wow, I am a dense oatmeal cookie. Thank you a ton good sir.
– Werewoof
Nov 19 '18 at 18:56
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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])
1
Oh wow, I am a dense oatmeal cookie. Thank you a ton good sir.
– Werewoof
Nov 19 '18 at 18:56
add a comment |
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])
1
Oh wow, I am a dense oatmeal cookie. Thank you a ton good sir.
– Werewoof
Nov 19 '18 at 18:56
add a comment |
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])
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])
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
In your second for loop (
for (var j in database.children)
) i believe it should readfor (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