Counting keys in the JSON












1















This is my json



    var json = {
"pages": [{
"name": "page1",
"elements": [{
"type": "text",
"name": "question1"
},
{
"type": "text",
"name": "question2"
},
{
"type": "text",
"name": "question3"
}
]
}]
};


Now I try to count this json with this code



var keyCount  = Object.keys(json).length;

console.log(keyCount);


The output is 1.



But all i need is count this




  • {
    "type": "text",
    "name": "question1",
    },


  • {
    "type": "text",
    "name": "question2",
    },


  • {
    "type": "text",
    "name": "question3",
    }


It's should be 3.
Can someone help me?



  var json = {
"pages": [
{
"name": "page1",
"elements": [
{
"type": "text",
"name": "question1",
},
{
"type": "text",
"name": "question2",
},
{
"type": "text",
"name": "question3",
}
]
},
{
"name": "page2",
"elements": [
{
"type": "text",
"name": "question4"
},
{
"type": "text",
"name": "question5"
},
{
"type": "text",
"name": "question6"
}
]
}
]
}


Now I has another question.



You will see the "name": "page1" now it's has a page1, page2



and when i count page it return me just 3.



Why the return of this not 6



 var count = json.pages[0].elements.length
console.log(count);


6 is from




  • { "type": "text", "name": "question1", },

  • { "type": "text", "name": "question2", },

  • { "type": "text", "name": "question3", }
    question1-3 from page1)


  • { "type": "text", "name": "question4", },


  • { "type": "text", "name": "question5", },

  • { "type": "text", "name": "question6", }
    (question4-6 from page2)










share|improve this question





























    1















    This is my json



        var json = {
    "pages": [{
    "name": "page1",
    "elements": [{
    "type": "text",
    "name": "question1"
    },
    {
    "type": "text",
    "name": "question2"
    },
    {
    "type": "text",
    "name": "question3"
    }
    ]
    }]
    };


    Now I try to count this json with this code



    var keyCount  = Object.keys(json).length;

    console.log(keyCount);


    The output is 1.



    But all i need is count this




    • {
      "type": "text",
      "name": "question1",
      },


    • {
      "type": "text",
      "name": "question2",
      },


    • {
      "type": "text",
      "name": "question3",
      }


    It's should be 3.
    Can someone help me?



      var json = {
    "pages": [
    {
    "name": "page1",
    "elements": [
    {
    "type": "text",
    "name": "question1",
    },
    {
    "type": "text",
    "name": "question2",
    },
    {
    "type": "text",
    "name": "question3",
    }
    ]
    },
    {
    "name": "page2",
    "elements": [
    {
    "type": "text",
    "name": "question4"
    },
    {
    "type": "text",
    "name": "question5"
    },
    {
    "type": "text",
    "name": "question6"
    }
    ]
    }
    ]
    }


    Now I has another question.



    You will see the "name": "page1" now it's has a page1, page2



    and when i count page it return me just 3.



    Why the return of this not 6



     var count = json.pages[0].elements.length
    console.log(count);


    6 is from




    • { "type": "text", "name": "question1", },

    • { "type": "text", "name": "question2", },

    • { "type": "text", "name": "question3", }
      question1-3 from page1)


    • { "type": "text", "name": "question4", },


    • { "type": "text", "name": "question5", },

    • { "type": "text", "name": "question6", }
      (question4-6 from page2)










    share|improve this question



























      1












      1








      1








      This is my json



          var json = {
      "pages": [{
      "name": "page1",
      "elements": [{
      "type": "text",
      "name": "question1"
      },
      {
      "type": "text",
      "name": "question2"
      },
      {
      "type": "text",
      "name": "question3"
      }
      ]
      }]
      };


      Now I try to count this json with this code



      var keyCount  = Object.keys(json).length;

      console.log(keyCount);


      The output is 1.



      But all i need is count this




      • {
        "type": "text",
        "name": "question1",
        },


      • {
        "type": "text",
        "name": "question2",
        },


      • {
        "type": "text",
        "name": "question3",
        }


      It's should be 3.
      Can someone help me?



        var json = {
      "pages": [
      {
      "name": "page1",
      "elements": [
      {
      "type": "text",
      "name": "question1",
      },
      {
      "type": "text",
      "name": "question2",
      },
      {
      "type": "text",
      "name": "question3",
      }
      ]
      },
      {
      "name": "page2",
      "elements": [
      {
      "type": "text",
      "name": "question4"
      },
      {
      "type": "text",
      "name": "question5"
      },
      {
      "type": "text",
      "name": "question6"
      }
      ]
      }
      ]
      }


      Now I has another question.



      You will see the "name": "page1" now it's has a page1, page2



      and when i count page it return me just 3.



      Why the return of this not 6



       var count = json.pages[0].elements.length
      console.log(count);


      6 is from




      • { "type": "text", "name": "question1", },

      • { "type": "text", "name": "question2", },

      • { "type": "text", "name": "question3", }
        question1-3 from page1)


      • { "type": "text", "name": "question4", },


      • { "type": "text", "name": "question5", },

      • { "type": "text", "name": "question6", }
        (question4-6 from page2)










      share|improve this question
















      This is my json



          var json = {
      "pages": [{
      "name": "page1",
      "elements": [{
      "type": "text",
      "name": "question1"
      },
      {
      "type": "text",
      "name": "question2"
      },
      {
      "type": "text",
      "name": "question3"
      }
      ]
      }]
      };


      Now I try to count this json with this code



      var keyCount  = Object.keys(json).length;

      console.log(keyCount);


      The output is 1.



      But all i need is count this




      • {
        "type": "text",
        "name": "question1",
        },


      • {
        "type": "text",
        "name": "question2",
        },


      • {
        "type": "text",
        "name": "question3",
        }


      It's should be 3.
      Can someone help me?



        var json = {
      "pages": [
      {
      "name": "page1",
      "elements": [
      {
      "type": "text",
      "name": "question1",
      },
      {
      "type": "text",
      "name": "question2",
      },
      {
      "type": "text",
      "name": "question3",
      }
      ]
      },
      {
      "name": "page2",
      "elements": [
      {
      "type": "text",
      "name": "question4"
      },
      {
      "type": "text",
      "name": "question5"
      },
      {
      "type": "text",
      "name": "question6"
      }
      ]
      }
      ]
      }


      Now I has another question.



      You will see the "name": "page1" now it's has a page1, page2



      and when i count page it return me just 3.



      Why the return of this not 6



       var count = json.pages[0].elements.length
      console.log(count);


      6 is from




      • { "type": "text", "name": "question1", },

      • { "type": "text", "name": "question2", },

      • { "type": "text", "name": "question3", }
        question1-3 from page1)


      • { "type": "text", "name": "question4", },


      • { "type": "text", "name": "question5", },

      • { "type": "text", "name": "question6", }
        (question4-6 from page2)







      json






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 3:35

























      asked Nov 22 '18 at 9:57







      user8693711































          2 Answers
          2






          active

          oldest

          votes


















          2














          Yes because you have only one key : page. Also, pages is an array, so you should access the first cell.



          Try counting inside it :



          var count = json.pages[0].elements.length



          I'd recommend using Jsoneditoronline for viewing easily JSON structures.






          share|improve this answer
























          • It's work Thank you.

            – user8693711
            Nov 22 '18 at 10:20











          • hey a has another question can you help me?

            – user8693711
            Nov 23 '18 at 3:09











          • If it's linked to this question yes. Else, please search if your question hasn't been answered already on this site, or post a new question :)

            – Arthur Attout
            Nov 23 '18 at 7:57



















          1














          You want to count the number of elements under the elements attribute. So you should do



          var keyCount = Object.keys(json.pages[0].elements).length;



          Also elements is an array so



          var keyCount = json.pages[0].elements.length



          will work too.






          share|improve this answer
























          • It's work too Thank you.

            – user8693711
            Nov 22 '18 at 10:20











          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%2f53428276%2fcounting-keys-in-the-json%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









          2














          Yes because you have only one key : page. Also, pages is an array, so you should access the first cell.



          Try counting inside it :



          var count = json.pages[0].elements.length



          I'd recommend using Jsoneditoronline for viewing easily JSON structures.






          share|improve this answer
























          • It's work Thank you.

            – user8693711
            Nov 22 '18 at 10:20











          • hey a has another question can you help me?

            – user8693711
            Nov 23 '18 at 3:09











          • If it's linked to this question yes. Else, please search if your question hasn't been answered already on this site, or post a new question :)

            – Arthur Attout
            Nov 23 '18 at 7:57
















          2














          Yes because you have only one key : page. Also, pages is an array, so you should access the first cell.



          Try counting inside it :



          var count = json.pages[0].elements.length



          I'd recommend using Jsoneditoronline for viewing easily JSON structures.






          share|improve this answer
























          • It's work Thank you.

            – user8693711
            Nov 22 '18 at 10:20











          • hey a has another question can you help me?

            – user8693711
            Nov 23 '18 at 3:09











          • If it's linked to this question yes. Else, please search if your question hasn't been answered already on this site, or post a new question :)

            – Arthur Attout
            Nov 23 '18 at 7:57














          2












          2








          2







          Yes because you have only one key : page. Also, pages is an array, so you should access the first cell.



          Try counting inside it :



          var count = json.pages[0].elements.length



          I'd recommend using Jsoneditoronline for viewing easily JSON structures.






          share|improve this answer













          Yes because you have only one key : page. Also, pages is an array, so you should access the first cell.



          Try counting inside it :



          var count = json.pages[0].elements.length



          I'd recommend using Jsoneditoronline for viewing easily JSON structures.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 10:01









          Arthur AttoutArthur Attout

          1,081524




          1,081524













          • It's work Thank you.

            – user8693711
            Nov 22 '18 at 10:20











          • hey a has another question can you help me?

            – user8693711
            Nov 23 '18 at 3:09











          • If it's linked to this question yes. Else, please search if your question hasn't been answered already on this site, or post a new question :)

            – Arthur Attout
            Nov 23 '18 at 7:57



















          • It's work Thank you.

            – user8693711
            Nov 22 '18 at 10:20











          • hey a has another question can you help me?

            – user8693711
            Nov 23 '18 at 3:09











          • If it's linked to this question yes. Else, please search if your question hasn't been answered already on this site, or post a new question :)

            – Arthur Attout
            Nov 23 '18 at 7:57

















          It's work Thank you.

          – user8693711
          Nov 22 '18 at 10:20





          It's work Thank you.

          – user8693711
          Nov 22 '18 at 10:20













          hey a has another question can you help me?

          – user8693711
          Nov 23 '18 at 3:09





          hey a has another question can you help me?

          – user8693711
          Nov 23 '18 at 3:09













          If it's linked to this question yes. Else, please search if your question hasn't been answered already on this site, or post a new question :)

          – Arthur Attout
          Nov 23 '18 at 7:57





          If it's linked to this question yes. Else, please search if your question hasn't been answered already on this site, or post a new question :)

          – Arthur Attout
          Nov 23 '18 at 7:57













          1














          You want to count the number of elements under the elements attribute. So you should do



          var keyCount = Object.keys(json.pages[0].elements).length;



          Also elements is an array so



          var keyCount = json.pages[0].elements.length



          will work too.






          share|improve this answer
























          • It's work too Thank you.

            – user8693711
            Nov 22 '18 at 10:20
















          1














          You want to count the number of elements under the elements attribute. So you should do



          var keyCount = Object.keys(json.pages[0].elements).length;



          Also elements is an array so



          var keyCount = json.pages[0].elements.length



          will work too.






          share|improve this answer
























          • It's work too Thank you.

            – user8693711
            Nov 22 '18 at 10:20














          1












          1








          1







          You want to count the number of elements under the elements attribute. So you should do



          var keyCount = Object.keys(json.pages[0].elements).length;



          Also elements is an array so



          var keyCount = json.pages[0].elements.length



          will work too.






          share|improve this answer













          You want to count the number of elements under the elements attribute. So you should do



          var keyCount = Object.keys(json.pages[0].elements).length;



          Also elements is an array so



          var keyCount = json.pages[0].elements.length



          will work too.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 10:01









          Umang GuptaUmang Gupta

          3,17511535




          3,17511535













          • It's work too Thank you.

            – user8693711
            Nov 22 '18 at 10:20



















          • It's work too Thank you.

            – user8693711
            Nov 22 '18 at 10:20

















          It's work too Thank you.

          – user8693711
          Nov 22 '18 at 10:20





          It's work too Thank you.

          – user8693711
          Nov 22 '18 at 10:20


















          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%2f53428276%2fcounting-keys-in-the-json%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

          Wiesbaden

          Marschland

          Dieringhausen