Load of Array of Object from Data File Not Working

Tags: #<Tag:0x00007efc602e8d68>

Okay I’ve tried, but cannot get it to work. I’m trying to load “Array of Object” data from a data file and it’s not working.

I’m using the code from the example in your guide (see link at the end) as shown below:
Handsontable.dom.addEvent(load, ‘click’, () => {
ajax(‘data/json/load.json’, ‘GET’, ‘’, res => {
const data = JSON.parse(res.response);
hot.loadData(data);
exampleConsole.innerText = ‘Data loaded’;
});
});

The only difference is instead of the “load.json” file containing an “Array of Array”, I am using an “Array of Objects” and it’s not working.

To be specific, instead of using “Array of Array” like below:
[
[ “”, “Tesla”, “Volvo”, “Toyota”, “Ford” ],
[ “2019”, 10, 11, 12, 13 ],
[ “2020”, 20, 11, 14, 13 ],
[ “2021”, 30, 15, 12, 13 ]
]

I’m using “Array of Objects” as below and it’s not working:
[
{ id: 1, name: ‘Ted Right’, address: ‘address’ },
{ id: 2, name: ‘Frank Honest’, address: ‘’ },
{ id: 3, name: ‘Joan Well’, address: ‘’ },
{ id: 4, name: ‘Gail Polite’, address: ‘’ },
{ id: 5, name: ‘Michael Fair’, address: ‘’}
]

Link to your example I’m referencing:
https://handsontable.com/docs/saving-data/#overview

Hi @ptownbro

by not working do you mean any of those results

  • example does not build (build error)
  • table does not load (Handsontable is not attached to the DIV element)
  • table is loaded but there is no data

Thank you for responding. By not working I mean that the data does not bind to the table. To explain…

Similar to your example, I have a Handsontable and a “Load Data” button that will load the table with data from an external data file once it’s clicked. That external data file contains data in the “Array of Objects” format (as I show in my OP). After I click the button, nothing is loaded into the table and the error message I recieve in the console is as follows:

Uncaught Error: loadData only accepts array of objects or array of arrays (string given)

I can load from the data file “Array of Arrays” and “JSON” data, but not “Array of Objects”

This error was mentioned in those two subjects


And it seems that you should parse the data.

I don’t believe you can use JSON.parse() in this example (if that is what you mean).

I’m using your “Array of Objects” data in your guide examples (link) shown below. As I said in my OP, the difference is instead of explicitly stating it in the script, it’s saved in an external file:

const data = [
  { id: 1, name: 'Ted Right', address: '' },
  { id: 2, name: 'Frank Honest', address: '' },
  { id: 3, name: 'Joan Well', address: '' },
  { id: 4, name: 'Gail Polite', address: '' },
  { id: 5, name: 'Michael Fair', address: '' },
];

That data is not a valid JSON format, it’s a javascript object (I believe). Therefore, JSON.parse() will fail.

If this cannot

If something does not work for you as it works in linked threads I will need to request a demo for us to debug. This way we will be able to replicate the issue and found the cause.

Hi @ptownbro

have you had time to create a demo or debug it once again? I would like to replicate the same issue but I need some tips on how to do that.