djm.im
djm.im

Reputation: 3323

How to deserialize JSON array with different types?

I have a JSON array like this:

[
  "0",
  {
    "number": 1,
    "field": "value"
  },
  {
    "number": 2,
    "field": "value"
  }
]

The first element to the array is a string.

Is it possible to deserialize it with FasterXML Jackson?

I know how to do it with different objects.
I need to use @JsonSubTypes (Here is an example https://stackoverflow.com/a/38877862/2564509)

The problem with this array is that the first element is String type.

Upvotes: 0

Views: 1852

Answers (1)

DwB
DwB

Reputation: 38290

Caveat: Your situation is an unfortunate edge case. As such, the solution is likely to be not wonderful.

This works, but is not wonderful:

  1. First, deserialize as a List<Object>. In your case, this will result in a List of three elements; String, LinkedHashMap, and LinkedHashMap
  2. Next, process each element in the array and, process based on the element type; a String will be your String element, a LinkedHashMap will be the representation of your number-field class.

Upvotes: 2

Related Questions