I have JSON looks like this
{
"description":
{
"html": "A remote code execution vulnerability exists in the way that the scripting engine handles objects in memory in Microsoft Edge. ...",
"text": "<p>A remote code execution vulnerability exists in the way that the scripting engine handles objects in memory in Microsoft Edge. ...</p>"
}
}
I extract the field description but it contains both html and text, while I'm only interesting in text field.
while (true)
{
//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String lines = null;
StringBuilder stringBuilder = new StringBuilder();
while ((lines = bufferedReader.readLine()) != null)
{
stringBuilder.append(lines);
}
bufferedReader.close();
result = stringBuilder.toString();
JSONParser parser = new JSONParser();
JSONObject json2 = (JSONObject) parser.parse(result);
if(methodType == MethodType.Retrieve_Vulnerability_info)
{
String scan_vuln_title= json2.get("title").toString();
String scan_vuln_severityScore = json2.get("severityScore").toString();
String scan_vuln_publishe_date = json2.get("published").toString();
String scan_vuln_description = json2.get("description").toString();
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setSeverityScore(scan_vuln_severityScore);
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setVulnerability_title(scan_vuln_title);
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setPublished_date(scan_vuln_publishe_date);
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setDescription(scan_vuln_description);
System.out.print("\n Rapid7 : Successful GET, vulnerabilities info of : "+ scan_vuln_title + " were retrieved" );
}
Is there a way how to extract text content only?
json2.get("description").get("text")should do the trick (assumingjson2repesents a map, JsonObject etc.) - you might need to add some parsing and some null checks but I'll leave that for you.json2? There are many different JSON parsers/libraries which provide different API so answer will depend on what you are using.textandhtmlare flipped, sincetextcontains<p>...</p>structure whilehtmlcontains only "rendered" data.