Build JSON with sub levels
I am trying to build JSON as below.
{
"nameInfo": {
"name": "Bhanu"
}
}
I tried below code and its working fine.
a = {}
a.nameInfo = {}
a.nameInfo.name="John"
console.log(a)
However, just checking if there is a better way to do the same thing. Do I always have to initialize a.nameInfo to empty object before adding a property under nameInfo?
javascript json
|
show 1 more comment
I am trying to build JSON as below.
{
"nameInfo": {
"name": "Bhanu"
}
}
I tried below code and its working fine.
a = {}
a.nameInfo = {}
a.nameInfo.name="John"
console.log(a)
However, just checking if there is a better way to do the same thing. Do I always have to initialize a.nameInfo to empty object before adding a property under nameInfo?
javascript json
1
a.nameInfo = { name: 'John' }
or directlya = { nameInfo: { name: 'John' } }
– quirimmo
Jan 2 at 21:07
This is sort of correct. When you are writing 'use strict' you should always declare your variables and avoid using them without at leastvar
orlet
because this is scoped to thewindow
otherwise.
– Moosecouture
Jan 2 at 21:11
3
You’re building an object, not JSON. If you want JSON, useJSON.stringify
.
– Heretic Monkey
Jan 2 at 21:12
@HereticMonkey good call. Words matter. But if we overlook the fact that Bhanu is newer to javascript JS Objects are similar to JSON in the same sense that JSON actually means JavaScript Object Notation.
– Moosecouture
Jan 2 at 21:15
@Moosecouture are numbers similar to strings? You could say “1.01” is in JavaScript number format, since you can parse it as a number. Also, when someone is new to a language is precisely the time to intervene so that bad habits don’t settle in.
– Heretic Monkey
Jan 2 at 21:23
|
show 1 more comment
I am trying to build JSON as below.
{
"nameInfo": {
"name": "Bhanu"
}
}
I tried below code and its working fine.
a = {}
a.nameInfo = {}
a.nameInfo.name="John"
console.log(a)
However, just checking if there is a better way to do the same thing. Do I always have to initialize a.nameInfo to empty object before adding a property under nameInfo?
javascript json
I am trying to build JSON as below.
{
"nameInfo": {
"name": "Bhanu"
}
}
I tried below code and its working fine.
a = {}
a.nameInfo = {}
a.nameInfo.name="John"
console.log(a)
However, just checking if there is a better way to do the same thing. Do I always have to initialize a.nameInfo to empty object before adding a property under nameInfo?
javascript json
javascript json
edited Jan 2 at 21:19
isherwood
37.5k1082112
37.5k1082112
asked Jan 2 at 21:06
BhanuBhanu
1917
1917
1
a.nameInfo = { name: 'John' }
or directlya = { nameInfo: { name: 'John' } }
– quirimmo
Jan 2 at 21:07
This is sort of correct. When you are writing 'use strict' you should always declare your variables and avoid using them without at leastvar
orlet
because this is scoped to thewindow
otherwise.
– Moosecouture
Jan 2 at 21:11
3
You’re building an object, not JSON. If you want JSON, useJSON.stringify
.
– Heretic Monkey
Jan 2 at 21:12
@HereticMonkey good call. Words matter. But if we overlook the fact that Bhanu is newer to javascript JS Objects are similar to JSON in the same sense that JSON actually means JavaScript Object Notation.
– Moosecouture
Jan 2 at 21:15
@Moosecouture are numbers similar to strings? You could say “1.01” is in JavaScript number format, since you can parse it as a number. Also, when someone is new to a language is precisely the time to intervene so that bad habits don’t settle in.
– Heretic Monkey
Jan 2 at 21:23
|
show 1 more comment
1
a.nameInfo = { name: 'John' }
or directlya = { nameInfo: { name: 'John' } }
– quirimmo
Jan 2 at 21:07
This is sort of correct. When you are writing 'use strict' you should always declare your variables and avoid using them without at leastvar
orlet
because this is scoped to thewindow
otherwise.
– Moosecouture
Jan 2 at 21:11
3
You’re building an object, not JSON. If you want JSON, useJSON.stringify
.
– Heretic Monkey
Jan 2 at 21:12
@HereticMonkey good call. Words matter. But if we overlook the fact that Bhanu is newer to javascript JS Objects are similar to JSON in the same sense that JSON actually means JavaScript Object Notation.
– Moosecouture
Jan 2 at 21:15
@Moosecouture are numbers similar to strings? You could say “1.01” is in JavaScript number format, since you can parse it as a number. Also, when someone is new to a language is precisely the time to intervene so that bad habits don’t settle in.
– Heretic Monkey
Jan 2 at 21:23
1
1
a.nameInfo = { name: 'John' }
or directly a = { nameInfo: { name: 'John' } }
– quirimmo
Jan 2 at 21:07
a.nameInfo = { name: 'John' }
or directly a = { nameInfo: { name: 'John' } }
– quirimmo
Jan 2 at 21:07
This is sort of correct. When you are writing 'use strict' you should always declare your variables and avoid using them without at least
var
or let
because this is scoped to the window
otherwise.– Moosecouture
Jan 2 at 21:11
This is sort of correct. When you are writing 'use strict' you should always declare your variables and avoid using them without at least
var
or let
because this is scoped to the window
otherwise.– Moosecouture
Jan 2 at 21:11
3
3
You’re building an object, not JSON. If you want JSON, use
JSON.stringify
.– Heretic Monkey
Jan 2 at 21:12
You’re building an object, not JSON. If you want JSON, use
JSON.stringify
.– Heretic Monkey
Jan 2 at 21:12
@HereticMonkey good call. Words matter. But if we overlook the fact that Bhanu is newer to javascript JS Objects are similar to JSON in the same sense that JSON actually means JavaScript Object Notation.
– Moosecouture
Jan 2 at 21:15
@HereticMonkey good call. Words matter. But if we overlook the fact that Bhanu is newer to javascript JS Objects are similar to JSON in the same sense that JSON actually means JavaScript Object Notation.
– Moosecouture
Jan 2 at 21:15
@Moosecouture are numbers similar to strings? You could say “1.01” is in JavaScript number format, since you can parse it as a number. Also, when someone is new to a language is precisely the time to intervene so that bad habits don’t settle in.
– Heretic Monkey
Jan 2 at 21:23
@Moosecouture are numbers similar to strings? You could say “1.01” is in JavaScript number format, since you can parse it as a number. Also, when someone is new to a language is precisely the time to intervene so that bad habits don’t settle in.
– Heretic Monkey
Jan 2 at 21:23
|
show 1 more comment
2 Answers
2
active
oldest
votes
Easiest way is to do the following:
let a = {
nameInfo: {
name: 'Bhanu'
}
}
maybe "easiest" is not the right word. Even his one is really easy. Let's say the "most compact way"
– quirimmo
Jan 2 at 21:10
Point taken. I think this is the Clearest and most Compact and DRY.
– Moosecouture
Jan 2 at 21:12
add a comment |
different way, still JS object
var a = { };
a['nameInfo'] = { name: 'Bhanu' };
console.log(a);
add a comment |
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%2f54013185%2fbuild-json-with-sub-levels%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Easiest way is to do the following:
let a = {
nameInfo: {
name: 'Bhanu'
}
}
maybe "easiest" is not the right word. Even his one is really easy. Let's say the "most compact way"
– quirimmo
Jan 2 at 21:10
Point taken. I think this is the Clearest and most Compact and DRY.
– Moosecouture
Jan 2 at 21:12
add a comment |
Easiest way is to do the following:
let a = {
nameInfo: {
name: 'Bhanu'
}
}
maybe "easiest" is not the right word. Even his one is really easy. Let's say the "most compact way"
– quirimmo
Jan 2 at 21:10
Point taken. I think this is the Clearest and most Compact and DRY.
– Moosecouture
Jan 2 at 21:12
add a comment |
Easiest way is to do the following:
let a = {
nameInfo: {
name: 'Bhanu'
}
}
Easiest way is to do the following:
let a = {
nameInfo: {
name: 'Bhanu'
}
}
answered Jan 2 at 21:09
MoosecoutureMoosecouture
30818
30818
maybe "easiest" is not the right word. Even his one is really easy. Let's say the "most compact way"
– quirimmo
Jan 2 at 21:10
Point taken. I think this is the Clearest and most Compact and DRY.
– Moosecouture
Jan 2 at 21:12
add a comment |
maybe "easiest" is not the right word. Even his one is really easy. Let's say the "most compact way"
– quirimmo
Jan 2 at 21:10
Point taken. I think this is the Clearest and most Compact and DRY.
– Moosecouture
Jan 2 at 21:12
maybe "easiest" is not the right word. Even his one is really easy. Let's say the "most compact way"
– quirimmo
Jan 2 at 21:10
maybe "easiest" is not the right word. Even his one is really easy. Let's say the "most compact way"
– quirimmo
Jan 2 at 21:10
Point taken. I think this is the Clearest and most Compact and DRY.
– Moosecouture
Jan 2 at 21:12
Point taken. I think this is the Clearest and most Compact and DRY.
– Moosecouture
Jan 2 at 21:12
add a comment |
different way, still JS object
var a = { };
a['nameInfo'] = { name: 'Bhanu' };
console.log(a);
add a comment |
different way, still JS object
var a = { };
a['nameInfo'] = { name: 'Bhanu' };
console.log(a);
add a comment |
different way, still JS object
var a = { };
a['nameInfo'] = { name: 'Bhanu' };
console.log(a);
different way, still JS object
var a = { };
a['nameInfo'] = { name: 'Bhanu' };
console.log(a);
var a = { };
a['nameInfo'] = { name: 'Bhanu' };
console.log(a);
var a = { };
a['nameInfo'] = { name: 'Bhanu' };
console.log(a);
answered Jan 2 at 21:24
MrJMrJ
8561212
8561212
add a comment |
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.
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%2f54013185%2fbuild-json-with-sub-levels%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
1
a.nameInfo = { name: 'John' }
or directlya = { nameInfo: { name: 'John' } }
– quirimmo
Jan 2 at 21:07
This is sort of correct. When you are writing 'use strict' you should always declare your variables and avoid using them without at least
var
orlet
because this is scoped to thewindow
otherwise.– Moosecouture
Jan 2 at 21:11
3
You’re building an object, not JSON. If you want JSON, use
JSON.stringify
.– Heretic Monkey
Jan 2 at 21:12
@HereticMonkey good call. Words matter. But if we overlook the fact that Bhanu is newer to javascript JS Objects are similar to JSON in the same sense that JSON actually means JavaScript Object Notation.
– Moosecouture
Jan 2 at 21:15
@Moosecouture are numbers similar to strings? You could say “1.01” is in JavaScript number format, since you can parse it as a number. Also, when someone is new to a language is precisely the time to intervene so that bad habits don’t settle in.
– Heretic Monkey
Jan 2 at 21:23