cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

json to infotable

JackEom
14-Alexandrite

json to infotable

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

1 REPLY 1

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.

Top Tags