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
- parse the HTML text into an object
- get the table from the response i.e. select("table").first();
- 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");