Skip to main content
1-Visitor
June 30, 2020
Solved

json to infotable

  • June 30, 2020
  • 1 reply
  • 1869 views

I get JSON type data.

output is

{
   "array": [
         {
             "times": [
                  {
                     "color": "#f2b827",
                     "ending_time": 1593478173523,
                     "extra": "Indischarging",
                     "starting_time": 1593475515628,
                     "percent": 2657895
                   },
                  {
                     "color": "#3ec557",
                     "ending_time": 1593475515628,
                     "extra": "Charging",
                     "starting_time": 1593442800000,
                     "percent": 32715628
                  }
              ]
            }
        ]
   }

 

 

So I'm going to make infotable with this data.

for(var i=0; i < json.times.length; i++) {
    infotabletest.AddRow({extra:json.times[i].extra,
                                        color:json.times[i].color,
                                        starting_time:json.times[i].starting_time,
                                        ending_time:json.times[i].ending_time,
                                        percent:json.times[i].percent});
  }
var result = infotabletest;

 

but I can't do that. output is "Error executing service test. Message :: TypeError: Cannot read property "length" from undefined.

 

Can you help me out? Thank you.

 

Best answer by VladimirRosu_116627

The error message you've got indicates the issue: "cannot read property length from undefined".

Since length is only used in the for loop, it means that the property "times" is undefined (does not exist) in the object "json".

When looking at your JSON object, I see the following structure:

-key name "array" with an array value-> you need to access its first element based on index -> array[0]

-the first element of the array contains a key name "times" with an array value -> now you can read its "length" property -> array[0].times.length

 

Note: You will need to modify accordingly also the code in the loop.

1 reply

19-Tanzanite
June 30, 2020

The error message you've got indicates the issue: "cannot read property length from undefined".

Since length is only used in the for loop, it means that the property "times" is undefined (does not exist) in the object "json".

When looking at your JSON object, I see the following structure:

-key name "array" with an array value-> you need to access its first element based on index -> array[0]

-the first element of the array contains a key name "times" with an array value -> now you can read its "length" property -> array[0].times.length

 

Note: You will need to modify accordingly also the code in the loop.