2

I've been trying many different things, but I cannot find a working solution how to find the number of results in an array which is nested in a JSON.

Here is the JSON:

{ 
   "Status":"OK",
   "ID":"xxx",
   "Results":[  ]
}

And this is the structure of the Results array:

[ 
   { 
      "Call":{ 
         "ID":1,
         "CustomerKey":null,
         "CustomerID":null
      },
      "PingID":4,
      "Key":null,
      "Properties":[ 
         { 
            "Name":"OrdinalID",
            "Value":"1"
         }
      ],
      "CreatedDate":"0001-01-01T00:00:00.000",
      "ID":0,
      "ObjectID":null
   }
]

I am trying to display results in a table. The table should have as many rows as there are results. But I am getting an incorrect number every time.

If I do JSON.length, I get 3, and if I do JSON.Results[0].length I get over 10, where the correct number of results/rows should be 4.

Any idea how to check the length of this?

4
  • do you want to know the length of JSON.Results array ? Commented Oct 24, 2019 at 10:35
  • What do you mean JSON.length? You cannot get the length of JSON like that. Commented Oct 24, 2019 at 10:37
  • 3
    If it is the Results array the one you're asking for, Results.length should do it? In any case we need a Minimal, Reproducible Example to know exactly how to help you better. There are things unclear in this post. Commented Oct 24, 2019 at 10:37
  • Let's assume that your Object (the JSON at the top) is assigned to a variable called foo. In order to retrieve the length of the Results array, you could simply use foo.Results.length Commented Oct 24, 2019 at 10:42

1 Answer 1

1

The correct way is to do JSON.results.length. (assuming JSON is the object where you stored the result from parsing).

JSON.length will give you 3 because you get the number of properties in the original object which is 3.

JSON.results[0].length will give you the length of the first element of the array. The first element of the array is that object you posted 2nd, and this is why you get a high number.

You're looking for the length of the results array in that JSON object, hence JSON.results.length will work.

Sign up to request clarification or add additional context in comments.

7 Comments

But JSON is a reserved name. He probably didn't use it as the object name because there'd be a conflict.
@TomaszKasperczyk yeah I meant it as a substitute name for the sake of the question since he used it that way I assumed, didn't think he really used it in his code.
@CodeRage np :) Can you please accept the answer with the green V so question is marked as answered?
@AbacusAbacus it's better to parse one time and then just use the JS object you get from it. parsing many times will probably not effect much in terms of processing time (unless results has huge number of entries), but it is redundant and not a good practice.
@AbacusAbacus also, this JSON.parse(myJSON.Results[i].PingID) - is wrong. you can't parse part of a JSON. in this case you'd have to do JSON.parse(myJSON).results[i].PingID
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.