erj2code
erj2code

Reputation: 311

How do I pull out the JSON field I want using Jackson TreeNode and JsonNode?

I'm a little stumped why I can't pull the "Type" field out of my JSON stream to make a decision. It seems like this should be so easy. I have the following JSON that I have as input:

[
{
"Institution":"ABC",
"Facility":"XYZ",
"Make":"Sunrise",
"Model":"Admission",
"SerialNumber":"",
"Revision":"1",
"Type":"ABC_Admission",
"ArchiveData":"<CSV file contents>"
}
]

In my Java I have a try-catch block with a JsonHolder class that implements Serializable to hold the JSON. Here's the Java I currently have:

try {
// Parse and split the input
JsonHolder data = JsonHolder.getField("text", input);
DataExtractor.LOG.info("JsonHolder data= " + data);
TreeNode node = data.getTreeNode();
DataExtractor.LOG.info("node size= " + node.size());
node = node.path("Type");
JsonNode json = (JsonNode) node;

DataExtractor.LOG.info("json= " + json.asText());

// code to decide what to do based on Type found
if (json.asText().equals("ABC_Admission")) {
 // do one thing
} else {
 // do something else
}

} catch (IOException iox) {
DataExtractor.LOG.error("Error extracting data", iox);
this.collector.fail(input);
}

When I run my code I get the following output (NOTE: I changed my package name where the class is to just for this output display)

25741 [Thread-91-DataExtractor] INFO <proprietary package name>.DataExtractor - JsonHolder data= [
{
"Institution":"ABC",
"Facility":"XYZ",
"Make":"Sunrise",
"Model":"Admission",
"SerialNumber":"",
"Revision":"1",
"Type":"ABC_Admission",
"ArchiveData":"<CSV file contents>"
}
]
25741 [Thread-91-DataExtractor] INFO <proprietary package name>.DataExtractor - node size= 1
25741 [Thread-91-DataExtractor] INFO <proprietary package name>.DataExtractor - json=

As you can see I don't get anything out. I just want to extract the value of the field "Type", so I was expecting to get the value "ABC_Admission" in this case. I would have thought the node path would separate out just that field from the rest of the JSON tree.
What am I doing wrong?

Upvotes: 0

Views: 2745

Answers (1)

erj2code
erj2code

Reputation: 311

After consulting with another developer I found out the issue is my JSON is inside an array. So, I need to iterate over that array and then pull out the Type field from the object.

The updated code to resolve this is below:

        try {
            // Parse and split the input
            JsonHolder data = JsonHolder.getField("text", input);
            DataExtractor.LOG.info("JsonHolder data= " + data);
            TreeNode node = data.getTreeNode();

            String type = null;

            // if this is an array of objects, iterate through the array
            // to get the object, and reference the field we want
            if (node.isArray()){
                ArrayNode ary = (ArrayNode) node;
                for (int i = 0; i < ary.size(); ++i) {
                    JsonNode obj = ary.get(i);
                    if (obj.has("Type")) {
                        type = obj.path("Type").asText();
                        break;
                    }
                }
            }

            if (type == null) {
               // Do something with failure??
            }

            DataExtractor.LOG.info("json= " + type);

            if (type.equals("ABC_Admission")) {
             // do one thing
            else {
             // do something else
            }

        } catch (IOException iox) {
            DataExtractor.LOG.error("Error extracting data", iox);
            this.collector.fail(input);
        }   

Upvotes: 1

Related Questions