DEV Community

loading...
Cover image for How to simply examine a JSON response from a Cloudant search in Java

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

Thomas Südbröcker
My personal guideline is: “You may fall down, but you have to get up again!”
・3 min read

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"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;  
Enter fullscreen mode Exit fullscreen mode

Code

Steps:

  1. Get Cloudant search response value
  2. Create simple JSON object from search response value
  3. Extract nested JSON array from simple JSON object
  4. Get the first JSON object entry in the JSON array
  5. Extract the nested JSON object from the first JSON object entry
  6. 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);
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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

Discussion (0)