How to simply examine a JSON response from a Cloudant search in Java

This is a short cheat sheet about, how to simply examine a JSON response from a Cloudant search in Java. I found different examples, but these examples were (more or less) older examples, where I missed some pieces and at the end for me the Java EE documentation was the best resource to realize it.
The JSON I wanted to examine, was a JSON with a nested JSON array and that array also contains a nested JSON.
JSON format of the Cloudant search response
Here you see is the JSON format I wanted to examine. As I said: It's a JSON with a nested JSON array and the array also contains a nested JSON.
{
  "total_rows": 1,
  "bookmark": "g2wAAAABaANkAClkYmNvcmVAZGI3LmJtLWNjLXVzLXNvdXRoLTExLmNsb3VkYW50Lm5ldGwAAAACYhAAAABiH____2poAkY_8AAAAAAAAGEAag",
  "rows": [
    {
      "fields": {
        "url": "https://haralduebele.github.io/2019/02/17/blue-cloud-mirror-dont-open-the-doors/",
        "customer": "your-blogs",
        "title": "Blue Cloud Mirror — (Don’t) Open The Doors!",
        "type": "articles",
        "authorName": "Harald Uebele"
      },
      "id": "7a5f27ce3ef66f895cb666e46ce45e55"
    }
  ]
}
This is the simply content I need from the JSON response of the Cloudant search:
  • AuthorName
  • Title
  • URL
  • Example in JavaScript format:
    rows[0].fields.authorName
    rows[0].fields.title
    rows[0].fields.url
    Java code and imports
    Here is the Java code and the imports I used.
    Imports
    // JSON
    import javax.json.Json;
    import javax.json.JsonObject;
    import javax.json.JsonArray;
    import javax.json.JsonReader;
    // Need to the string input
    import java.io.StringReader;
    Code
    Steps:
  • Get Cloudant search response value
  • Create simple JSON object from search response value
  • Extract nested JSON array from simple JSON object
  • Get the first JSON object entry in the JSON array
  • Extract the nested JSON object from the first JSON object entry
  • Extract the relevant data from the nested JSON object.
  • // Cloudant search response value
    System.out.println("-->log: search_response.toString: " + search_response.toString());
    
    // Create simple json object from response value
    JsonReader jsonReader = Json.createReader(new StringReader(search_response.toString()));
    JsonObject object = jsonReader.readObject();
    jsonReader.close();
    
    // Extract nested json array from simple json object 
    JsonArray rows = object.getJsonArray("rows");
    System.out.println("-->log: rows: " + rows);
    
    // Get the first json object entry in the json array 
    JsonObject row_object = rows.getJsonObject(0);
    System.out.println("-->log: row_object: " + row_object);
    
    // Extract the nested json object from the first json object entry
    JsonObject fields = row_object.getJsonObject("fields");
    System.out.println("-->log:  fields: " + fields);
    
    // Extract the relevant data from the nested json object
    String url = fields.getString("url");
    String authorName = fields.getString("authorName");
    String title = fields.getString("title");
    System.out.println("-->log: Author : " + authorName + " Title: " + " url: " + url);
    Console output for the example
    This is an example console output of the running application.
    -->log: search_response.toString: {
      "total_rows": 1,
      "bookmark": "g2wAAAABaANkAClkYmNvcmVAZGI3LmJtLWNjLXVzLXNvdXRoLTExLmNsb3VkYW50Lm5ldGwAAAACYhAAAABiH____2poAkY_8AAAAAAAAGEAag",
      "rows": [
        {
          "fields": {
            "url": "https://haralduebele.github.io/2019/02/17/blue-cloud-mirror-dont-open-the-doors/",
            "customer": "your-blogs",
            "title": "Blue Cloud Mirror — (Don’t) Open The Doors!",
            "type": "articles",
            "authorName": "Harald Uebele"
          },
          "id": "7a5f27ce3ef66f895cb666e46ce45e55"
        }
      ]
    }
    -->log: rows: [{"fields":{"url":"https://haralduebele.github.io/2019/02/17/blue-cloud-mirror-dont-open-the-doors/","customer":"your-blogs","title":"Blue Cloud Mirror — (Don’t) Open The Doors!","type":"articles","authorName":"Harald Uebele"},"id":"7a5f27ce3ef66f895cb666e46ce45e55"}]
    -->log: row_object: {"fields":{"url":"https://haralduebele.github.io/2019/02/17/blue-cloud-mirror-dont-open-the-doors/","customer":"your-blogs","title":"Blue Cloud Mirror — (Don’t) Open The Doors!","type":"articles","authorName":"Harald Uebele"},"id":"7a5f27ce3ef66f895cb666e46ce45e55"}
    -->log:  fields: {"url":"https://haralduebele.github.io/2019/02/17/blue-cloud-mirror-dont-open-the-doors/","customer":"your-blogs","title":"Blue Cloud Mirror — (Don’t) Open The Doors!","type":"articles","authorName":"Harald Uebele"}
    -->log: Author : Harald Uebele Title:  url: https://haralduebele.github.io/2019/02/17/blue-cloud-mirror-dont-open-the-doors/
    Additional useful links :
    Here are some additional links, which can give you an additional point of view.
    Other useful links:
    Here are some additional links, which can give you an additional point of view.
    Summary
    Finally it's pretty easy to handle JSON in Java. I hope this was useful for you and let’s see what’s next?
    Greetings,
    Thomas

    19

    This website collects cookies to deliver better user experience

    How to simply examine a JSON response from a Cloudant search in Java