How to convert tabular string to JSON using Node.js ?
Last Updated :
24 Jul, 2024
Tabular: Information that is displayed in a table with rows and columns is known as "tabular format". A data table is an organized and practical approach to displaying a significant amount of information that contains repeated data items. The majority of office productivity software packages, including word processing software and spreadsheets, have capabilities for text and data entry in tabular format.
Example:
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>A1</td>
<td>A2</td>
<td>A3</td>
</tr>
<tr>
<td>B1</td>
<td>B2</td>
<td>B3</td>
</tr>
<tr>
<td>C1</td>
<td>C2</td>
<td>C3</td>
</tr>
</tbody>
</table>
JSON: JavaScript Object Notation, or JSON. It is a text-based data transfer format that preserves the data's organizational structure. It supports data structures that are quickly executed on the server, such as arrays, objects, and JSON documents. It is also a JavaScript-derived language-independent format. JSON files should be saved with the .json extension and the application/json media type.
Example:
[
[
{ 'Column 1': 'A1', 'Column 2': 'A2', 'Column 3': 'A3' },
{ 'Column 1': 'B1', 'Column 2': 'B2', 'Column 3': 'B3' },
{ 'Column 1': 'C1', 'Column 2': 'C2', 'Column 3': 'C3' }
]
]
Approach: We need one NPM Package for converting tabular format or string to JSON named "tabletojson".
npm install tabletojson
The method convert() and convertUrl() in this package allows us to pass the markup for a single table as a string, a chunk of HTML, a whole page, and just a URL (with an optional callback function; promises are also supported). Every time, an array is a response. A table from the page is represented by each array entry in the response (in same the order they were found in the HTML).
Example 1: In this example, we will be using convertUrl() with a remote URL that contains any tabular form data for demonstrating the conversion of the table to JSON. First, We will be importing a class from tabletojson module named Tabletojson class and storing it into a variable called tabletojson. This convertUrl() method will be called from this class reference variable. This convertUrl() method will take two parameters as given below:
Parameters:
- The first parameter will be a remote URL or Web URL which contains any tabular form data.
- The second Parameter will be a callback function that will get executed after the completion of the conversion of tabular format data to JSON successfully.
Project Structure: It will look like the following.
ouapp.js
JavaScript
const tabletojson = require('tabletojson').Tabletojson;
tabletojson.convertUrl(
'https://en.wikipedia.org/wiki/Tata_Consultancy_Services',
function (tablesAsJson) {
console.log(tablesAsJson[1]);
}
);
Step to run the application: Open the terminal and type the following command.
node app.js
Example 2: In this example, we will use a local HTML file that contains some data in tabular format for demonstrating an example of the conversion of the table to JSON data. First, We need to import the class Tabletojson from tabletojson module in the app.js file. In this class, there is a method convert which we will be using for converting tabular data to JSON. Before, starting the implementation of code, we need to understand two methods that we will use in implementation. The first method is fs.reafFileSync() and the Second method is path.resolve(). Below, briefly explained both methods with their syntax, parameters and it's return values.
Using fs.readFileSync() method: The fs module includes an application programming interface (API) called fs.readFileSync() that can be used to read files and return their contents.
Syntax:
fs.readFileSync( path, options )
Parameters:
- path: It uses the text file's relative path. A URL-type path is also possible. Additionally, the file may be a file descriptor. Just put the filename in quotes if both files are in the same folder.
- options: It is an optional parameter that contains an encoding, a flag, and a data definition in the encoding. Its default value of null returns a raw buffer, and the flag includes information about file actions. 'r' is the default value for it.
Return Value: This method will return the content of the file.
Using path.resolve() method: A series of route segments can be resolved to an absolute path using the path.resolve() method. It operates by prepending each of the paths in the right-to-left order of the paths until the absolute path is produced. The resulting path is normalized, and any necessary trailing slashes are taken out.
Syntax:
path.resolve( [...paths] )
Parameters:
- paths: It consists of a collection of file paths that would be combined to make an absolute path. If this parameter's value is not a string, a TypeError is raised.
Return Value: This method will return a string with an absolute path.
Project Structure:
table.html file: Using this HTML file for sample data for showing demonstration in the example.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>GeeksforGeeks</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>A1</td>
<td>A2</td>
<td>A3</td>
</tr>
<tr>
<td>B1</td>
<td>B2</td>
<td>B3</td>
</tr>
<tr>
<td>C1</td>
<td>C2</td>
<td>C3</td>
</tr>
</tbody>
</table>
</body>
</html>
app.js file:
JavaScript
const tabletojson = require('tabletojson').Tabletojson;
const fs = require('fs');
const path = require('path');
const html = fs.readFileSync(path.resolve(__dirname, 'table.html'),
{ encoding: 'UTF-8' });
const converted = tabletojson.convert(html);
console.log(converted);
Output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read