Build JSON with sub levels












0















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?










share|improve this question




















  • 1





    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






  • 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
















0















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?










share|improve this question




















  • 1





    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






  • 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














0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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






  • 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














  • 1





    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






  • 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








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












2 Answers
2






active

oldest

votes


















1














Easiest way is to do the following:



let a = {
nameInfo: {
name: 'Bhanu'
}
}





share|improve this answer
























  • 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



















0














different way, still JS object






var a = { };
a['nameInfo'] = { name: 'Bhanu' };

console.log(a);








share|improve this answer
























    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%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









    1














    Easiest way is to do the following:



    let a = {
    nameInfo: {
    name: 'Bhanu'
    }
    }





    share|improve this answer
























    • 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
















    1














    Easiest way is to do the following:



    let a = {
    nameInfo: {
    name: 'Bhanu'
    }
    }





    share|improve this answer
























    • 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














    1












    1








    1







    Easiest way is to do the following:



    let a = {
    nameInfo: {
    name: 'Bhanu'
    }
    }





    share|improve this answer













    Easiest way is to do the following:



    let a = {
    nameInfo: {
    name: 'Bhanu'
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













    0














    different way, still JS object






    var a = { };
    a['nameInfo'] = { name: 'Bhanu' };

    console.log(a);








    share|improve this answer




























      0














      different way, still JS object






      var a = { };
      a['nameInfo'] = { name: 'Bhanu' };

      console.log(a);








      share|improve this answer


























        0












        0








        0







        different way, still JS object






        var a = { };
        a['nameInfo'] = { name: 'Bhanu' };

        console.log(a);








        share|improve this answer













        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);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 21:24









        MrJMrJ

        8561212




        8561212






























            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.




            draft saved


            draft discarded














            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





















































            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

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]