Could someone please tell me if there is a way to change a string into an array for use in organizing Tabulator.js

I have developed a Google Application Script that generates data for Tabulator.js from a Google Sheet as a string. When I insert this text directly into my HTML file, it works fine. However, when I try to load it via a Google Application Script call and assign the string to the Tabulator's data variable, I encounter an error message:

Data Loading Error - Unable to process data due to invalid data type Expecting: array , Received:  string 

Is there a straightforward way to convert this string into the required JSON array structure? Please note the use of children in the first row. You can experiment with it on jsFiddle.

I have tried using JSON.parse, but it seems like the syntax of the string is not correct:

SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data"

[
  {id:1, name:"BalanceOil", _children:
    [
      {id:2, name:"BalanceOil+  s testy", cena:31},
      {id:3, name:"BalanceOil+ ", cena:31}
    ]
  },
  {id:11, name:"Xtend x2 Kit", cena:23},
  {id:18, name:"Viva+ Kit", cena:21}
]

The original data appears more complex, but I have simplified it here.

[{id:1, name:"BalanceOil", _children:
    [{id:2, name:"BalanceOil+  s testy", cena:31, mn:1,cena_1:"", package:159, kredityMesicne:4, kredityBalik:14},
     {id:3, name:"BalanceOil+ ", cena:31, mn:1,cena_1:"", package:85, kredityMesicne:4, kredityBalik:8},]},
 {id:11, name:"Xtend x2 Kit", cena:23, mn:1,cena_1:"", package:83, kredityMesicne:3, kredityBalik:8},
 {id:18, name:"Viva+ Kit", cena:21, mn:1,cena_1:"", package:60, kredityMesicne:3, kredityBalik:4}
]

Can I use the data source as a string, or do I need to modify the code and create the structure as the desired object type? Should it be an array or JSON format?

Answer №1

Various Methods to Accomplish This

  1. eval can be a risky method if the input is not completely reliable as it involves executing dynamic code.

// Sample code snippet from JSFiddle
var data ='[{id:1, name:"BalanceOil", _children:[{id:2, name:"BalanceOil+  s testy", cena:31, mn:1,cena_1:"", package:159, kredityMesicne:4, kredityBalik:14},{id:3, name:"BalanceOil+ ", cena:31, mn:1,cena_1:"", package:85, kredityMesicne:4, kredityBalik:8},]},{id:11, name:"Xtend x2 Kit", cena:23, mn:1,cena_1:"", package:83, kredityMesicne:3, kredityBalik:8},{id:18, name:"Viva+ Kit", cena:21, mn:1,cena_1:"", package:60, kredityMesicne:3, kredityBalik:4}]';
eval(data);

  1. Incorporate manual parsing of the string to create an array/object: String to object in JS

  2. Modify the script to output actual JSON instead of a string

Note: The code snippet may not execute here due to security measures, but you can try running it in the console

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Searching for documents in MongoDB that meet specific criteria has become possible through the use

Criteria: COUNT the total number of documents in the collection WHERE objects.objectType is 'group' AND (objects.objectType is NOT 'person' AND relation is 'Exposed_to') Expectation: should return the count of all documents W ...

Include performers in a roster for DVD collection

Currently, I am facing an issue with adding multiple actors to a DVD object. Despite my efforts to use JavaScript to dynamically create text boxes for each actor, only the first one is being saved. Do you have any advice or suggestions on how to resolve th ...

What is the best way to serialize all li#id elements within an ul using jQuery's serialize() function

First and foremost, I want to clarify that I am utilizing the latest version of jQuery and not jQuery-UI. Below is the list I am working with: <ul id="audio_list"> <li id="trackid_1"></li> <li id="trackid_5"></li> ...

Exploring the differences between a callback function and an asynchronous

In the code snippet below, I have implemented handling for SIGINT and SIGTERM signals in a node.js application. // quit on ctrl+c when running docker in terminal process.on('SIGINT', async () => { console.info('Got SIGINT (aka ctrl+c in ...

I'm facing an issue with binding keyboard events in Vue - any suggestions on how to resolve

Hello everyone! I'm a newcomer to the world of Vue. Recently, I encountered an issue with keyboard-event binding that has left me puzzled. Let me share the relevant code snippet below: ...other code... <template v-for="(ite ...

The button was not generated due to an error with the regular expression flags

I keep encountering an error message Error: Invalid regular expression flags Every time I click on the edit button, this error pops up. However, the HTML output appears to be correct /* EDIT */ { mRender: function (data, type, row) { var d ...

The Angular @Input directive may be prone to receiving inaccurate model data

I am currently working on setting up @Input for my component using a model that resembles the following: interface Car { sail?: never tires: number weight: number } interface Boat { tires?: never sail: boolean weight: number } exp ...

Using Facebook authentication in React Native

Currently developing a React Native app and aiming to incorporate Facebook login functionality. The back-end logic for user authentication is handled by an API in Node.js. Utilizing passport.js to enable users to log in using either Facebook or Email cre ...

Having trouble with AngularJS UI Router failing to load template and controller?

I am currently trying to identify the error in my code. Whenever I access /dashboard, it only loads the template from the first route, which is defined as SYSTEM_VARS.includes.general.root. However, it does display the console.log message inside the resolv ...

Error in Angular 7: Trying to assign a 'void' type to a 'ObservableInput<{}>' type

I can't seem to understand why I am encountering this error fetchFromURL(url: string): any { return this.apiService.get(url).pipe(map(data => data)); } fetchNextPage(): Observable<any> { const url = 'url'; this.f ...

Using Three JS to recycle the geometry of a previously imported object

Is there a way to efficiently re-use imported object geometries in Three JS without duplicating them in memory? I've tried writing a loader but it doesn't seem to update the geometry once loaded. var tmpGeo = geometries[ID]; if (!tmpGeo) { t ...

Transform JavaScript AJAX to HttpWebRequest implementation code

Is it possible to mimic an AJAX call to a web service within a console application using HttpWebRequest? Here is the source request: var webRequest = Sys.Net.WebServiceProxy.invoke('', 'MyMethod', false, {p1:aa,p2:bb,p3:123}, onSucc ...

How can I transfer data from my iPhone to my computer?

Currently, I have successfully built an iPhone app using PhoneGap and AngularJS that scans and processes barcodes exactly how I want. However, I also have a webapp powered by AngularJS that requires barcode input and similar processing capabilities. Since ...

Is there a way to intercept all AJAX requests in JavaScript?

Could there be a universal event handler for ajax requests that is automatically triggered upon the completion of any ajax request? This is something I am exploring while developing a greasemonkey script for an ajax-heavy website. In past scripts, I have ...

Is there a way to create an event listener that responds to a simultaneous click of both mouse buttons?

Despite my extensive research on the Internet, I was unable to find any examples. Interestingly, Vue only supports right and left clicks separately which I find peculiar as it seems like a basic task that can easily be accomplished with plain Javascript. ...

Generating unique ObjectIDs for each object is crucial before saving documents in Mongoose

I need to generate a unique ObjectID for every object within my array. The challenge is that I am fetching products from another server using a .forEach statement and adding them to my array without a Schema that automatically creates an ObjectID.... Prod ...

How can I create an image that spans the entire width of the screen, Swiper?

var swiper = new Swiper('.swiper-container', { pagination: { el: '.swiper-pagination', }, }); html, body { position: relative; height: 100%; } body { background: #eee; font-fami ...

What methods are available to access a variable beyond the event listener scope?

Is there a way to create a JavaScript code that generates a list of selected pictures, including the thumbnail, filename, and filesize of each picture? I've been trying, but it seems like the event listener isn't able to access the filename and f ...

When utilizing JqGrid, make sure to utilize the 'aftersavefunc' and 'successfunc' methods prior to executing the 'saveRow' function

After saving a row in order to retrieve the Id for logging, I am encountering an issue where 'aftersavefunc' and 'successfunc' are triggering 'reloadGrid('#telephoneGrid')' before the save operation. It seems that th ...

Tips for retrieving data using ajax with servlets:

I am trying to send some data on the page to a servlet So I have created the following jQuery code to achieve this I am using all the data to construct a JSON string and send it directly to the servlet However, I am unsure how to retrieve the entire dat ...