Can you build the array as an object before writing it, rather than bothering with all the individual pieces?

ObjectMapper mapper = new ObjectMapper();
ArrayNode array = mapper.createArrayNode();

int i = 0;
while (i < 6) {
    array.add(mapper.createArrayNode().add("" + i++).add("" + i++));
}
System.out.println(array);

Results in:

[["0","1"],["2","3"],["4","5"]]

If you're not dealing with several megabytes of data or very tight memory constraints, this might turn out to be more maintainable as well.

Answer from Chris on Stack Overflow
🌐
Attacomsian
attacomsian.com › blog › jackson-create-json-array
How to create a JSON array using Jackson
October 14, 2022 - The above code will generate the following JSON array: [ { "name" : "Ford", "models" : [ "Fiesta", "Focus", "Mustang" ] }, { "name" : "BMW", "models" : [ "320", "X3", "X5" ] }, { "name" : "Fiat", "models" : [ "500", "Panda" ] } ] For more Jackson ...
Discussions

java - How to use Jackson to deserialise an array of objects - Stack Overflow
This is an excellent solution for the simple case of a root json array! 2019-03-28T17:37:28.1Z+00:00 ... Save this answer. ... Show activity on this post. ... This answer worked for me: Java 11, Jackson 2.10.5. More on stackoverflow.com
🌐 stackoverflow.com
java - How to parse a JSON string to an array using Jackson - Stack Overflow
Which is the simplest way to do it using Jackson ObjectMapper? ... I had a similar situation , but decided to de-serialise to JSONArray object instead of doing it to a class, since it would avoid class serialisation issues in future. More on stackoverflow.com
🌐 stackoverflow.com
How to parse Json array with 2 or more different types using Jackson?

http://www.baeldung.com/jackson-inheritance

You'll need to have those types inherit from one base class though.

Also; that is really bad JSON.

More on reddit.com
🌐 r/javahelp
11
4
January 15, 2018
java - Serializing array with Jackson - Stack Overflow
If you are using Jackson, then the following simple example worked for me. ... public class Foo { private List fooElements = Arrays.asList("one", "two", "three"); public Foo() { } public List getFooElements() { return fooElements; } } ... import java.io.IOException; import org.codehaus.jackson.JsonGenerationException... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Baeldung
baeldung.com › home › json › jackson › jackson – unmarshall to collection/array
Jackson - Unmarshall to Collection/Array | Baeldung
April 26, 2024 - If you want to dig deeper and learn other cool things you can do with the Jackson 2 – head on over to the main Jackson tutorial. ... @Test public void givenJsonArray_whenDeserializingAsArray_thenCorrect() throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); List<MyDto> listOfDtos = Lists.newArrayList( new MyDto("a", 1, true), new MyDto("bc", 3, false)); String jsonArray = mapper.writeValueAsString(listOfDtos); // [{"stringValue":"a","intValue":1,"booleanValue":true}, // {"stringValue":"bc","intValue":3,"booleanValue":false}] MyDto[] asArray = mapper.readValue(jsonArray, MyDto[].class); assertThat(asArray[0], instanceOf(MyDto.class)); }
🌐
Mkyong
mkyong.com › home › java › how to parse json array with jackson
How to parse JSON Array with Jackson | mkyong.com
April 23, 2024 - package com.mkyong.json.jackson; ... convert JSON array to Array objects Person[] person1 = mapper.readValue(jsonArray, Person[].class); for (Person p : person1) { System.out.println(p); } // 2....
🌐
GeeksforGeeks
geeksforgeeks.org › java › deserialize-json-array-to-a-single-java-object-with-jackson
Deserialize JSON Array to a Single Java Object with Jackson - GeeksforGeeks
July 23, 2025 - ... package org.example; import ... an array of Course objects String json = "[{\n" + " \"id\": 1,\n" + " \"courseName\": \"Introduction to Java\",\n" + " \"coursePrice\": 30000.99\n" + "}\n]"; //...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › how-can-we-convert-a-json-array-to-a-list-using-jackson-in-java
How can we convert a JSON array to a list using Jackson in Java?
June 5, 2025 - import com.fasterxml.jackson.c... JsonArrayToList{ public static void main(String[] args) { String jsonArray = "[{"name":"John","age":30},{"name":"Jane","age":25}]"; ObjectMapper objectMapper = new ObjectMapper(); try { List<Person> ...
🌐
Sourcecodeexamples
sourcecodeexamples.net › 2020 › 01 › jackson-convert-json-array-to-java-list.html
Jackson - Convert JSON Array to Java List Object
November 6, 2023 - ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); String json = "[ \"C\", \"C++\", \"Java\", \"Java EE\", \"Python\", \"Scala\", \"JavaScript\" ]"; List < String > progLangs = new ArrayList < > (); progLangs = mapper.readValue(json, List.class); for (Iterator < String > iterator = progLangs.iterator(); iterator.hasNext();) { String progLang = (String) iterator.next(); System.out.println(progLang); } } } ... Java Record Class Example Java Text Blocks Example Java Record Builder Pattern Example Java Record Constructor Example Java Record Constructor Val
🌐
Medium
medium.com › @Mohd_Aamir_17 › mastering-json-in-java-a-comprehensive-guide-to-handling-json-objects-arrays-and-nodes-with-df57bf0ebff1
Mastering JSON in Java: A Comprehensive Guide to Handling JSON Objects, Arrays, and Nodes with Jackson | by Mohd Aamir | Medium
November 4, 2024 - A JSON Array is an ordered collection of values, which may be objects, strings, numbers, or other primitives. Jackson’s ArrayNode class, which extends JsonNode, is specifically for handling JSON arrays.
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-objectmapper.html
Jackson ObjectMapper
The Jackson ObjectMapper can also read a Java List of objects from a JSON array string. Here is an example of reading a List of objects from a JSON array string:
🌐
Stack Abuse
stackabuse.com › converting-json-array-to-a-java-array-or-list
Convert JSON Array to a Java Array or List with Jackson
September 8, 2020 - Using Jackson's ObjectMapper class, it's easy to read values and map them to an object, or an array of objects. We just use the readValue() method, passing the JSON contents and the class we'd like to map to.
🌐
Attacomsian
attacomsian.com › blog › jackson-convert-json-array-to-from-java-list
Convert JSON array to a list using Jackson in Java
November 6, 2022 - try { // create a list of users List<User> users = Arrays.asList( new User("John Doe", "john.doe@example.com", new String[]{"Member", "Admin"}, true), new User("Tom Lee", "tom.lee@example.com", new String[]{"Member"}, false) ); // convert users list to JSON file new ObjectMapper().writeValue(Paths.get("users.json").toFile(), users); } catch (Exception ex) { ex.printStackTrace(); } For more Jackson examples, check out the How to read and write JSON using Jackson in Java tutorial.
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › java json › convert json array to java list using jackson
Convert JSON Array to Java List using Jackson - Apps Developer Blog
March 22, 2024 - Converting a JSON array to a list manually involves using Java Jackson’s ObjectMapper class and iterating through the JSON array elements. Here’s a step-by-step process: ... In this example, jsonArrayString represents the JSON array in string format.
🌐
Makeinjava
makeinjava.com › home › convert array of objects to / from json – jackson objectmapper
Convert array of Objects to/from JSON - jackson objectMapper
January 1, 2024 - Convert JSON String to array of ... converted to JSON Person[] personList = Stream.of( new Person("Mike", "harvey", 34), new Person("Nick", "young", 75), new Person("Jack", "slater", 21 ), new Person("gary", "hudson", 55)) .toArray(Person[]::new); //1....
🌐
Reddit
reddit.com › r/javahelp › how to parse json array with 2 or more different types using jackson?
r/javahelp on Reddit: How to parse Json array with 2 or more different types using Jackson?
January 15, 2018 -

Hi guys, I've been searching online and stackoverflow but can't seem to find an answer to this. Basically the API i'm calling returns a json array as the root object. The two objects inside this array are different types. I have defined two Java classes to represent these two types and I'm wanting to read the json and convert them into my java object with jackson. Seems like there isn't a way to do this??

simple example of my objects:

class Type1 {
  String name
}

class Type2 {
  long age
}

The json coming from the api looks like this:

[
 {
    "name" : "mickey"
  },
  {
    "age" : 100
  }
]

How can I tell jackson to read in this "tree" but convert it to some object that is holding these two?

EDIT Here is a reply I made to another comment:

Ok, it seems like nobody is understanding what I'm saying. For one, I don't have control of the api I'm calling. I get what I get and have to deal with it.

What that api returns is a json ARRAY as the ROOT object. There are two objects in that array. Those two objects are NOT the same types.

IF that array was holding objects of the same type, i could do this:

MyObjects[] myobjs = new Objectmapper().readValue("some json array here", MyObjects[].class);

But like I said, i can't do this. Because the array is not holding objects of the same type. It would be more akin to a tuple like in python.

🌐
GitHub
gist.github.com › adityasatrio › fc8e63319ea18e7e4f92
Example Json Mapper (jackson) - Json Array without Class Object Description · GitHub
example json object : [{"c_dikum":"SMA","c_tahun":"2009"},{"c_dikum":"SMP","c_tahun":"2006"},{"c_dikum":"SD","c_tahun":"2003"}]