162

I am changing my JSON library from org.json to Jackson and I want to migrate the following code:

JSONObject datasets = readJSON(new URL(DATASETS));
JSONArray datasetArray =  datasets.getJSONArray("datasets");

Now in Jackson I have the following:

ObjectMapper m = new ObjectMapper();
JsonNode datasets = m.readTree(new URL(DATASETS));      
ArrayNode datasetArray = (ArrayNode)datasets.get("datasets");

However I don't like the cast there, is there the possibility for a ClassCastException? Is there a method equivalent to getJSONArray in org.json so that I have proper error handling in case it isn't an array?

Marco
  • 1,377
  • 15
  • 18
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • Unfortunately I cannot use full mapping because the data does not fixed field names. – Konrad Höffner May 28 '13 at 09:23
  • 1
    If the field names come from a limited set you may want to define a class featuring all of them, and use the deserializer's `FAIL_ON_UNKNOWN_PROPERTIES` feature to just get nulls returned in the unused fields. But that's of course only an option if the field name set is relatively limited. – fvu May 28 '13 at 09:28
  • Hm I think this solution doesn't fit best in my case but I will remember it in case I will have a problem with a limited set which is known in advance! – Konrad Höffner May 29 '13 at 08:54

5 Answers5

311

Yes, the Jackson manual parser design is quite different from other libraries. In particular, you will notice that JsonNode has most of the functions that you would typically associate with array nodes from other APIs. As such, you do not need to cast to an ArrayNode to use. Here's an example:

JSON:

{
    "objects" : ["One", "Two", "Three"]
}

Code:

final String json = "{\"objects\" : [\"One\", \"Two\", \"Three\"]}";

final JsonNode arrNode = new ObjectMapper().readTree(json).get("objects");
if (arrNode.isArray()) {
    for (final JsonNode objNode : arrNode) {
        System.out.println(objNode);
    }
}

Output:

"One"
"Two"
"Three"

Note the use of isArray to verify that the node is actually an array before iterating. The check is not necessary if you are absolutely confident in your data structure, but it's available should you need it (and this is no different from most other JSON libraries).

Saikat
  • 14,222
  • 20
  • 104
  • 125
Perception
  • 79,279
  • 19
  • 185
  • 195
  • May I know why "final" is used in the line "for (final JsonNode objNode : arrNode) "? – Anthony Vinay May 25 '20 at 14:01
  • 2
    @AnthonyVinay To make the variables immutable; many programmers consider this a good coding practice. See also [this](https://softwareengineering.stackexchange.com/questions/98691/excessive-use-final-keyword-in-java) discussion! – Jacob van Lingen Aug 13 '21 at 06:51
24

In Java 8 you can do it like this:

import java.util.*;
import java.util.stream.*;

List<JsonNode> datasets = StreamSupport
    .stream(obj.get("datasets").spliterator(), false)
    .collect(Collectors.toList())
Ori Popowski
  • 10,432
  • 15
  • 57
  • 79
5

I would assume at the end of the day you want to consume the data in the ArrayNode by iterating it. For that:

Iterator<JsonNode> iterator = datasets.withArray("datasets").elements();
while (iterator.hasNext()) 
        System.out.print(iterator.next().toString() + " "); 

or if you're into streams and lambda functions:

import com.google.common.collect.Streams;
Streams.stream(datasets.withArray("datasets").elements())
    .forEach( item -> System.out.print(item.toString()) )
Wildhammer
  • 2,017
  • 1
  • 27
  • 33
1

Is there a method equivalent to getJSONArray in org.json so that I have proper error handling in case it isn't an array?

It depends on your input; i.e. the stuff you fetch from the URL. If the value of the "datasets" attribute is an associative array rather than a plain array, you will get a ClassCastException.

But then again, the correctness of your old version also depends on the input. In the situation where your new version throws a ClassCastException, the old version will throw JSONException. Reference: http://www.json.org/javadoc/org/json/JSONObject.html#getJSONArray(java.lang.String)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Ah ok so I could just catch a ClassCastException, thanks! For my taste it is a bit less elegant than having a specific JsonException but if is not possible otherwise that is still good. – Konrad Höffner May 29 '13 at 08:50
1

Obtain an iterator by calling the JsonNode's iterator() method, and go on...

  JsonNode array = datasets.get("datasets");

  if (array.isArray()) {
      Iterator<JsonNode> itr = array.iterator();
      /* Set up a loop that makes a call to hasNext().
      Have the loop iterate as long as hasNext() returns true.*/
      while (itr.hasNext()) {
          JsonNode item=itr.next();
          // do something with array elements
      }
  }
ucMedia
  • 4,105
  • 4
  • 38
  • 46