What is the proper method for transforming an Excel column containing JSON format data into a JavaScript object?

I am facing an issue with converting data from an excel sheet to json format. While the other columns convert successfully, one specific column containing json data is not being converted properly and instead gets treated as a string. Using JSON.parse() on this column results in a Syntax Error. I have attempted the following solutions:

  • Trying to convert the excel sheet to json using the xlsx package
  • Converting the excel file to csv first and then converting it to json using the csvtojson package

Unfortunately, neither of these methods has helped me in successfully converting the problematic column into a valid json object. Below is the excerpt from my code:

let xlsx = require("xlsx")
const csvtojson = require("csvtojson")
let path = require("path")
let fs = require("fs");

const inputFilePath = path.join(__dirname, './mastersheet.xlsx');
let File = xlsx.readFile(inputFilePath);
let content = xlsx.utils.sheet_to_csv(File.Sheets['Sheet6']);

fs.writeFile('./mastersheet.csv', content, (err) => {
    if (err) console.log(err)
})

csvtojson()
    .fromFile('./mastersheet.csv')
    .then((jsonObj) => {
         console.log(jsonObj);
         fs.writeFileSync("mastersheet.json", JSON.stringify(validJsonData), 'utf8', (err) => {
             if (err) console.log(err);
         })
    });

Any suggestions or assistance on resolving this issue would be greatly appreciated.

Answer №1

If you're looking to convert Excel files to JSON, the convert-excel-to-json library can help you achieve that easily. Here's a simple example of how you can use it:

const excelToJson = require('convert-excel-to-json');
const fs = require('fs');

const result = excelToJson({
    source: fs.readFileSync('example.xlsx'),
    header:{
        rows: 1
    }
});
console.log('output:', result);

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

Is there a way to streamline the process of dragging and dropping multiple files with C# selenium automation?

I have successfully implemented code to drag and drop a single image, but I am unsure about the necessary changes needed to support multiple file uploads. Can anyone provide assistance with this? Thank you. driver.Url = "http://example.com"; IWebEleme ...

Tips for personalizing the css styles of an alert box?

I am in need of customizing the alert box using CSS attributes. Currently, I am able to achieve a similar effect with the code below: JQUERY: $('<div class="alertMessage">Error first</div>') .insertAfter($('#componentName' ...

In what way can a container impact the appearance of a child placed in the default slots?

Visiting the vue playground. The main goal is for the container component to have control over an unspecified number of child components in the default slot. In order to achieve this, it's assumed that each child component must either hold a propert ...

Using Angular Ionic for a click event that is triggered by a specific class

I am utilizing Highcharts and would like to click on the legend upon loading. With the use of Angular Ionic, how can I trigger a click on the .highcharts-legend-item class within the ngOnInit() {} method? I am aiming to click on this class as soon as the ...

Enhancing the current Node.js, Express, MongoDB, and Socket.io stack with the integration of AngularJS

After spending some time developing a web app using Node.js, Express, MongoDB, Mongoose and Socket.io, I've successfully released version one. Looking ahead to version two, my plan is to revamp the UI completely and switch to a front-end framework lik ...

Engaging Floor Layout

Currently, I am in the process of developing an interactive floorplan for an office project. The main objective is to provide a user-friendly interface where individuals can search for a specific employee and have their respective office location highlight ...

transferring data from JavaScript to PHP variables

Currently, I am working on a project that involves using ajax to access the database asynchronously. The value retrieved is stored in a JavaScript variable like this: var content=xmlhttp.responseText; My next step is to pass this value to the PHP module ...

Determine the total value of various dynamic elements by utilizing jQuery/Javascript for calculation

I am currently working on a project that involves a dynamic list of products in a cart. I need to calculate the total price of all the products in the cart, but I am having trouble figuring out how to access and calculate the values from these dynamic elem ...

In MongoDB, learn the process of efficiently updating nested objects in a dynamic manner

We have a variety of nested configurations stored in MongoDB. Initially, we store the following object in MongoDB: const value = { 'a': { 'b': 1 } } collection.insertOne({userId, value}) Now, I would like to modify the object in ...

Executing a series of HTTP requests sequentially using Angular 5

I need some guidance on sending an array of HTTP requests in sequential order within my application. Here are the details: Application Entities : Location - an entity with attributes: FanZone fanZone, and List<LocationAdministrator> locationAdmins ...

Discover properties of a TypeScript class with an existing object

I am currently working on a project where I need to extract all the properties of a class from an object that is created as an instance of this class. My goal is to create a versatile admin page that can be used for any entity that is associated with it. ...

Connecting a Vue js model data to a Select2 select box

Utilizing select2 to improve an html select element, I am facing challenges in binding the value of the select element to a Vue variable because Select2 appears to be causing interference. Is there an optimal approach to achieve this data binding and even ...

Running the `npm build` command does not automatically execute the "build" script specified in the package.json file

I am exploring a new approach for my module by utilizing npm build instead of relying on gulp / Grunt or other specialized build tools. "scripts": { "build": "node build.js" }, The content of my build.js file is simply: console.log('Hello') ...

What is the best method for converting a JObject to an array in VB.net?

How can I convert a JObject into a specific property list? Below is the JSON data I have: { "response":{ "result":1, "resultcount":1, "collectiondetails":[ { "publishedfileid&qu ...

Is there a way to unselect a button in Angular?

I have a set of buttons representing different categories. When I click on a button, it displays a card with relevant information. <app-category-button [label]='category.name' [isSelected]='category.id === (selectedCategoryId$ | asy ...

I'm looking to extract the actor names from the JSON provided below. While I've been successful in retrieving other values, the actor names elude me

This is the format of my JSON response. I was able to extract the values of id and title, but encountered difficulty with the actor field as it contains another array within the JSON object. Below is the React code I have implemented. Despite trying variou ...

Function in jQuery to reference two different divs

I'm currently facing an issue with a code snippet that I have. The requirement is for the user to be able to hover over "Div 1" and/or "Div2" and trigger a red border around both elements simultaneously. Due to the complexity of my WordPress theme, th ...

Can an older style class be inherited from an ECMAScript 6 class in JavaScript?

When I run the code below on Node.js version 4.2.1: 'use strict'; var util = require('util'); class MyClass { constructor(name) { this.name = name; } } function MyDerived() { MyClass.call(this, 'MyDerived'); } ...

The error message reads: `json.decoder.JSONDecodeError: Unexpected additional data present at line 2, starting from column 1 (character

I encountered an error: (json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 5357)) when trying to parse a JSON file. Can someone explain the reason behind this error? Additionally, could you provide guidance on how to properly extract va ...

Ways to examine attributes assigned in the controller.$onInit function in AngularJS

In a certain scenario, I am initializing some variables in the controller's $onInit method. How can I verify that these properties are being set correctly? Controller ctrl.$onInit = function () { ctrl.devices = _.cloneDeep(ctrl.formDevices); }; ...