0

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?

5
  • 1
    Well, since it is nested json something like json2.get("description").get("text") should do the trick (assuming json2 repesents a map, JsonObject etc.) - you might need to add some parsing and some null checks but I'll leave that for you. Commented Jun 21, 2019 at 12:41
  • What is the type of json2? There are many different JSON parsers/libraries which provide different API so answer will depend on what you are using. Commented Jun 21, 2019 at 12:43
  • I'm using SimpleJSON library, I have added the full code, I don't have the mond get("text") Commented Jun 21, 2019 at 12:48
  • Aside from main question: it seems that your text and html are flipped, since text contains <p>...</p> structure while html contains only "rendered" data. Commented Jun 21, 2019 at 12:51
  • this is returned from the Server side - I think they are flipped as well Commented Jun 21, 2019 at 12:56

2 Answers 2

1

Instead of extracting to a String, extract the description as a JSON object.
so what your code would look like is this,

JSONObject json3 = json2.getJSONObject("description")

then,

String html = json3.get("html")
String text = json3.get("text")

Also, a quick reminder, I'm using org.json

Edit1: Since you are using simple.json

JSONObject json2 = (JSONObject) object.get("description");
String html = (String) json2.get("html");
String text = (String) json2.get("text");
Sign up to request clarification or add additional context in comments.

1 Comment

JSONObject json2 = (JSONObject) object.get("description"); , object is not known.
0

The best way is to use JsonPath click here to know more with working example.

1 Comment

I don't want to use another library!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.