eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Introduction

In this tutorial, we’ll explore how to convert JSON data to Apache Avro objects in Java. Avro is a data serialization framework that provides rich data structures and binary data in a compact format. In addition, unlike other serialization frameworks, Avro uses schemas defined in JSON format, instead of requiring code generation for serialization.

As a consequence, one of its key strengths is support for schema evolution. This way, Avro is particularly suitable for applications that need to handle data structures that change over time. Furthermore, thanks to its compact data format it’s useful for applications processing high volumes of data.

2. JSON to Avro Conversion

In Avro, converting from JSON to objects requires a schema that establishes the data structure and a conversion mechanism. In our case, this conversion mechanism will be in the convertJsonToAvro() method.

The schema defines the format of the data (including field names and types), while the method uses this schema to transform the JSON into Avro objects.

2.1. Implementing the Conversion Method

First, let’s add the necessary dependency to our pom.xml:

<dependency>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro</artifactId>
    <version>1.12.0</version>
</dependency>

Next, let’s create a schema that defines the structure our JSON should follow:

private static final String SCHEMA_JSON = """
    {
        "type": "record",
        "name": "Customer",
        "namespace": "com.baeldung.avro",
        "fields": [
            {"name": "name", "type": "string"},
            {"name": "age", "type": "int"},
            {"name": "email", "type": ["null", "string"], "default": null}
        ]
    }""";

Next, let’s create a converter method that handles the JSON to Avro transformation. Furthermore, the conversion process involves three main components: the schema, a decoder that reads JSON data according to the schema, and a DatumReader that creates the Avro objects.

Let’s create the method:

GenericRecord convertJsonToAvro(String json) throws IOException {
    
    try {
        DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
        Decoder decoder = DecoderFactory.get().jsonDecoder(schema, json);
        return reader.read(null, decoder);
    } catch (IOException e) {
        throw new IOException("Error converting JSON to Avro", e);
    }
}

Using the decoder we’ve instantiated we read the JSON input and make sure this matches our schema structure. Finally, with the DatumReader we’re using both the schema and decoder to create GenericRecord objects. This way, Avro represents data without needing generated classes.

When converting JSON to Avro, we normally follow these steps:

  • The JSON input is validated against the schema
  • The decoder parses the JSON according to the schema’s structure
  • The DatumReader creates a GenericRecord containing the data
  • Any fields not present in the JSON but defined in the schema are assigned their default values

One important aspect to note is how Avro handles union types. When we define a field as a union (for example: [“null”, “string”]), the JSON representation must explicitly specify which type is being used. For example, we must wrap a string value in a JSON object with the type as the key: {“string”: “value”}. As such, this differs from regular JSON where we’re just using the value directly.

2.2. Testing the JSON to Avro Conversion

Now, let’s test our implementation:

@Test
void whenValidJsonInput_thenConvertsToAvro() throws IOException {
    
    JsonToAvroConverter converter = new JsonToAvroConverter();
    String json = "{\"name\":\"John Doe\",\"age\":30,\"email\":{\"string\":\"[email protected]\"}}";

    GenericRecord record = converter.convertJsonToAvro(json);

    assertEquals("John Doe", record.get("name").toString());
    assertEquals(30, record.get("age"));
    assertEquals("[email protected]", record.get("email").toString());
}

This test verifies that our converter handles correctly a complete JSON object (every field is populated). Furthermore, let’s note the special format for the email field. This uses Avro’s union-type syntax.

As we can see from the test, all types are correctly converted and accessible in the GenericRecord result variable.

Let’s take a look at our next test, where we transform a JSON with a null field into a GenericRecord:

@Test
void whenJsonWithNullableField_thenConvertsToAvro() throws IOException {
    
    JsonToAvroConverter converter = new JsonToAvroConverter();
    String json = "{\"name\":\"John Doe\",\"age\":30,\"email\":null}";

    GenericRecord record = converter.convertJsonToAvro(json);

    assertEquals("John Doe", record.get("name").toString());
    assertEquals(30, record.get("age"));
    assertNull(record.get("email"));
}

This test confirms that we’re converting properly the null value in the optional email field. We’ve defined the email field as a union of [“null”, “string”] in our schema. Therefore, it accepts null values.

3. Advanced Usage

With the basic conversion from JSON to Avro object we’re covering many usual cases. However, real-world applications often require more complex operations.

Let’s look at two scenarios: processing JSON arrays (essential for data set handling) and binary serialization. The latter prepares the data for storage or communication.

3.1. Processing JSON Arrays

Sometimes, we’ll need to process multiple JSON objects at once. Considering this, let’s extend our converter to handle JSON arrays. For this, let’s create a new method:

List<GenericRecord> convertJsonArrayToAvro(String jsonArray) throws IOException {
    
    List<GenericRecord> records = new ArrayList<>();
    
    Schema arraySchema = Schema.createArray(schema);
    
    Decoder decoder = DecoderFactory.get().jsonDecoder(arraySchema, jsonArray);
    DatumReader<List<GenericRecord>> reader = new GenericDatumReader<>(arraySchema);
    
    List<GenericRecord> result = reader.read(null, decoder);
    return result;
}

Now, let’s analyze our method. First, we’re creating a schema for an array of our existing record schema. Next, we’re using Avro’s built-in JSON decoder to verify that our JSON (“arraySchema) respects the structure defined in the schema, converts each field to its schema equivalent, and then, handle accordingly special cases such as the union types.

Finally, we’re using the DatumReader and we’re reading the entire array at once.

3.2. Testing the Processing of JSON Arrays

Now, let’s create a test to verify this method:

@Test
void whenJsonArray_thenConvertsToAvroList() throws IOException {
    
    JsonToAvroConverter converter = new JsonToAvroConverter();
    String jsonArray = """
        [
            {"name":"John Doe","age":30,"email":{"string":"[email protected]"}},
            {"name":"Jane Doe","age":28,"email":{"string":"[email protected]"}}
        ]""";

    List<GenericRecord> records = converter.convertJsonArrayToAvro(jsonArray);

    assertEquals(2, records.size());
    assertEquals("John Doe", records.get(0).get("name").toString());
    assertEquals("[email protected]", records.get(1).get("email").toString());
}

Let’s briefly analyze the test. One thing worth mentioning is that for fields defined as unions (like our email field), we need to maintain the proper Avro JSON format, even within arrays.

3.3. Binary Serialization

While JSON to Avro objects is useful for data processing, most applications need storage or transfer of this data in an efficient format. As such, Avro’s binary serialization offers significant advantages over JSON or XML.

Some of these advantages are a more compact format, better serialization/deserialization performance, and built-in support for schema evolution.  Now, let’s write a method that helps us with this. Our serializeAvroRecord() method demonstrates how to convert a GenericRecord into its binary equivalent, ready for storage or transfer:

byte[] serializeAvroRecord(GenericRecord record) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
    BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
    
    writer.write(record, encoder);
    encoder.flush();
    return outputStream.toByteArray();
}

3.4. Testing the Binary Serialization

Now, let’s create a test to verify this method:

@Test
void whenSerializingAvroRecord_thenProducesByteArray() throws IOException {
    String json = """ 
        {"name":"John Doe","age":30,"email":{"string":"[email protected]"}}
        """ ;
    JsonToAvroConverter converter = new JsonToAvroConverter();
    GenericRecord record = converter.convertJsonToAvro(json);

    byte[] bytes = converter.serializeAvroRecord(record);

    assertNotNull(bytes);
    assertTrue(bytes.length > 0);
}

Let’s briefly analyze the test. First, we convert a JSON into a GenericRecord. Next, we serialize this record in binary format using our method, serializeAvroRecord(). Finally, we test that our method produces an array of bytes non-null and non-empty.

4. Conclusion

In this article, we’ve explored how to convert JSON data to Avro objects in Java. We’ve discussed basic conversion, handling arrays, serialization, and validation.

We’ve also talked about the importance of using Avro’s binary serialization abilities in favor of other options. Our solution provides a robust foundation for working with JSON and Avro in Java applications.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments