Open In App

How to convert tabular string to JSON using Node.js ?

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:

Project Structure: It will look like the following. 

ou

app.js




const tabletojson = require('tabletojson').Tabletojson;
  
tabletojson.convertUrl(
    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:  

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:

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. 




<!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: 




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:

 


Article Tags :