How to Extract Text from JSON Data Containing HTML Output by Nexpose
While viewing vulnerability results in Nexpose, I encountered JSON data containing HTML tags. Such data is difficult to read as-is, and requires text conversion to extract only the necessary information. In this article, I will explain how to use Python’s BeautifulSoup library to extract only the text from HTML data within JSON.
Background and Challenge
Nexpose report outputs sometimes include detailed security inspection information and results written in HTML format. Because this mixes HTML tags into the JSON data, the need arises to remove unwanted tag information and handle only the text data during analysis, log viewing, or report creation.
Here is an example:
data = {
"resource": [
{"results": [
{"proof": "<div>これは <strong>aaa</strong> です</div>"}
]},
{"results": [
{"proof": "こちらは bbb です"}
]}
]
}
Solution: Text Extraction with BeautifulSoup
By using Python’s BeautifulSoup library, you can easily extract only the text from HTML content. Below is a specific code example.
# Process to extract text from the HTML of each proof in the JSON data and update it
for item in data["resource"]:
for result in item.get("results", []):
if "proof" not in result:
continue # Skip if the proof key does not exist
result["proof"] = BeautifulSoup(result["proof"], 'html.parser').get_text()
In this code, we first loop through each results list in the JSON data and check if the proof key exists. If it does, we parse the HTML using BeautifulSoup and extract only the text portion using get_text(). Then, we overwrite the value of proof with the extracted text.
Applications and Precautions
- Error Handling: Exception handling and key existence checks are implemented in case the JSON data does not have the expected structure.
- Handling Large-Scale Data: When dealing with large volumes of data, consider batch processing or parallel processing while paying attention to processing speed and memory usage.
- Security Measures: Since incoming HTML may contain unexpected content, it is best to execute this only from trusted data sources.
Conclusion
JSON data containing HTML obtained from Nexpose can be converted into readable and practical information when processed correctly. We hope you will find the BeautifulSoup-based method introduced here useful for more efficient data processing and report generation.