0

I am facing a problem with the following payload.

The payload I am receiving on my Node.js backend on API call is as follows.

{
  "id": "1",
  "content":{
     "text": "
         <body>
           <div>
             <table>
               <thead>
                <tr>
                    <th><strong>Header 1</strong></th>
                    <th><strong>Header 2</strong></th>
                </tr>
               </thead>
                <tbody>
                 <tr>
                    <td>question 1</td>
                    <td>answer 1</td>
                 </tr>
                 <tr>
                    <td>question 2</td>
                    <td>answer 2</td>
                 </tr>
                 <tr>
                    <td>question 3</td>
                    <td>answer 3</td>
                 </tr>
                </tbody>
             </table>
           </div>
         </body>
         "
   }
}

so here I am storing the response as following:

var response = data

i.e. data is the JSON response

And I am saving the HTML data as follows

var HTMLContentText = response.content.text

As a result I will get this:

         <body>
           <div>
             <table>
               <thead>
                <tr>
                    <th><strong>Header 1</strong></th>
                    <th><strong>Header 2</strong></th>
                </tr>
               </thead>
                <tbody>
                 <tr>
                    <td>question 1</td>
                    <td>answer 1</td>
                 </tr>
                 <tr>
                    <td>question 2</td>
                    <td>answer 2</td>
                 </tr>
                 <tr>
                    <td>question 3</td>
                    <td>answer 3</td>
                 </tr>
                </tbody>
             </table>
           </div>
         </body>

Here I want to perform the following operations

  1. parse the HTML text into an object
  2. get the table from the response i.e. select("table").first();
  3. get the rows of the table i.e. select("tr")

I am having the same code in JAVA.

Here it is just for reference. Here, I used Jsoup parser for doing all the operations. I want to perform all in Javascript now.

        // HTML as text (from JSON)
        String HtmlFormattedText = (String)((JSONObject)JsonObject.get("content")).get("text");

        // parse the html text into an object
        Document HtmlFormattedDocumentObject = Jsoup.parse(HtmlFormattedText);

        // get the table from the response
        Element allRowsFromTable = HtmlFormattedDocumentObject.select("table").first();

        // get the rows of the table
        return allRowsFromTable.select("tr");
muka.gergely
  • 8,063
  • 2
  • 17
  • 34
Siddharth Thakor
  • 156
  • 3
  • 17

1 Answers1

2

I created a snippet for this. The return value of the function contains all tr elements of the table - you don't need to select the table first.

const response = {
  "id": "1",
  "content": {
    "text": `
         <body>
           <div>
             <table>
               <thead>
                <tr>
                    <th><strong>Header 1</strong></th>
                    <th><strong>Header 2</strong></th>
                </tr>
               </thead>
                <tbody>
                 <tr>
                    <td>question 1</td>
                    <td>answer 1</td>
                 </tr>
                 <tr>
                    <td>question 2</td>
                    <td>answer 2</td>
                 </tr>
                 <tr>
                    <td>question 3</td>
                    <td>answer 3</td>
                 </tr>
                </tbody>
             </table>
           </div>
         </body>
         `
  }
}

// logging the result of the function
console.log(parseTable(response.content.text))

function parseTable(string) {
  // creating an HTML element
  const el = document.createElement('html')

  // adding the string to the HTML element
  el.innerHTML = string

  // selecting and returning all tr elements
  return el.getElementsByTagName('tr')
}

Note: I changed the single quote to backtick in your data, as it allows multiline strings.

muka.gergely
  • 8,063
  • 2
  • 17
  • 34