---
schema_version: "1"
id: 1b13fa41c7caac622fc5f63b3df4320c6dff4b86527fc04d051f4ea82cbbbfc1
title: Java Format
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.4/dataweave-formats-java.md
canonical_url: https://docs.mulesoft.com/dataweave/2.4/dataweave-formats-java
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 375cac76c2feb8740a075dfee443251d7aa17fcdb2052b27b67e368d33b07abd
description: MuleSoft Documentation Site
release: "2.4"
latest: false
---
---
product: DataWeave
version: 2.4
keywords: format, java, */java, application/java
---

# Java Format

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

MIME type: `application/java`

ID: `java`

For the Java data format, DataWeave attempts to map any Java value to a DataWeave value, most often
by matching the semantics of DataWeave and Java.

## Java Value Mapping

The following table maps Java classes to DataWeave types.

<table><colgroup><col> <col></colgroup><thead><tr><th>Java Class</th><th>DataWeave</th></tr></thead><tbody><tr><td><p><code>java.lang.String</code></p></td><td><p><code>String</code></p></td></tr><tr><td><p><code>Enum</code></p></td><td><p><code>String</code></p></td></tr><tr><td><p><code>Class</code></p></td><td><p><code>String</code></p></td></tr><tr><td><p><code>UUID</code></p></td><td><p><code>String</code></p></td></tr><tr><td><p><code>CharSequence</code></p></td><td><p><code>String</code></p></td></tr><tr><td><p><code>Char</code></p></td><td><p><code>String</code></p></td></tr><tr><td><p><code>java.sql.Clob</code></p></td><td><p><code>String</code></p></td></tr><tr><td><p><code>java.io.Reader</code></p></td><td><p><code>String</code></p></td></tr><tr><td><p><code>int</code></p></td><td><p><code>Number</code></p></td></tr><tr><td><p><code>long</code></p></td><td><p><code>Number</code></p></td></tr><tr><td><p><code>double</code></p></td><td><p><code>Number</code></p></td></tr><tr><td><p><code>short</code></p></td><td><p><code>Number</code></p></td></tr><tr><td><p><code>BigInteger</code></p></td><td><p><code>Number</code></p></td></tr><tr><td><p><code>BigDecimal</code></p></td><td><p><code>Number</code></p></td></tr><tr><td><p><code>AtomicInteger</code></p></td><td><p><code>Number</code></p></td></tr><tr><td><p><code>AtomicLong</code></p></td><td><p><code>Number</code></p></td></tr><tr><td><p><code>boolean</code></p></td><td><p><code>Boolean</code></p></td></tr><tr><td><p><code>AtomicBoolean</code></p></td><td><p><code>Boolean</code></p></td></tr><tr><td><p><code>java.util.Collection</code></p></td><td><p><code>Array</code></p></td></tr><tr><td><p><code>java.lang.Iterable</code></p></td><td><p><code>Array</code></p></td></tr><tr><td><p><code>java.util.Iterator</code></p></td><td><p><code>Array</code></p></td></tr><tr><td><p><code>java.util.Map</code></p></td><td><p><code>Object</code></p></td></tr><tr><td><p><code>java.util.Optional</code></p></td><td><p><code>Null</code> or <code>Value</code></p></td></tr><tr><td><p><code>java.util.OptionalInt</code></p></td><td><p><code>Null</code> or <code>Number</code></p></td></tr><tr><td><p><code>java.util.OptionalDouble</code></p></td><td><p><code>Null</code> or <code>Number</code></p></td></tr><tr><td><p><code>java.util.OptionalLong</code></p></td><td><p><code>Null</code> or <code>Number</code></p></td></tr><tr><td><p><code>byte[]</code></p></td><td><p><code>Binary</code></p></td></tr><tr><td><p><code>java.lang.InputStream</code></p></td><td><p><code>Binary</code></p></td></tr><tr><td><p><code>java.nio.ByteBuffer</code></p></td><td><p><code>Binary</code></p></td></tr><tr><td><p><code>byte</code></p></td><td><p><code>Binary</code></p></td></tr><tr><td><p><code>java.lang.File</code></p></td><td><p><code>Binary</code></p></td></tr><tr><td><p><code>java.time.Instant</code></p></td><td><p><code>LocalDateTime</code></p></td></tr><tr><td><p><code>java.time.LocalDateTime</code></p></td><td><p><code>LocalDateTime</code></p></td></tr><tr><td><p><code>java.sql.Timestamp</code></p></td><td><p><code>LocalDateTime</code></p></td></tr><tr><td><p><code>java.sql.Date</code></p></td><td><p><code>LocalDateTime</code></p></td></tr><tr><td><p><code>java.util.Date</code></p></td><td><p><code>LocalDateTime</code></p></td></tr><tr><td><p><code>java.time.ZonedDateTime</code></p></td><td><p><code>DateTime</code></p></td></tr><tr><td><p><code>java.time.LocalTime</code></p></td><td><p><code>LocalTime</code></p></td></tr><tr><td><p><code>java.time.OffsetTime</code></p></td><td><p><code>Time</code></p></td></tr><tr><td><p><code>java.time.LocalDate</code></p></td><td><p><code>Date</code></p></td></tr><tr><td><p><code>java.time.ZoneOffset</code></p></td><td><p><code>TimeZone</code></p></td></tr><tr><td><p><code>java.util.Calendar</code></p></td><td><p><code>DateTime</code></p></td></tr><tr><td><p><code>java.util.XMLGregorianCalendar</code></p></td><td><p><code>DateTime</code></p></td></tr><tr><td><p><code>*[]</code></p></td><td><p><code>Array</code></p></td></tr></tbody></table>

> [!IMPORTANT] If a Java class is not in present in the table, DataWeave treats it as a `JavaBean`
> and maps it as an `Object` type. In this case, DataWeave takes all properties from the Java
> getters.

## Metadata

DataWeave supports the `^class` metadata for the Java format.

All Java objects are associated with a class. DataWeave maintains this association by mapping the
class to a Metadata property.

`payload.^class` returns the name of the class of the `payload`.

The Java writer uses the metadata property to discover the Java class to which the DataWeave value
maps. That is, DataWeave uses the metadata property to determine which Java class is to be created
from a given DataWeave value. For example, the expression
`output application/java --- now() as DateTime {class: "java.util.Calendar"}` creates a
`java.util.Calendar`.

## Enum Custom Type

The Java format supports the `Enum` custom type. To put a Java `enum` value into a `java.util.Map`,
the DataWeave Java module defines a custom type called `Enum`. This custom type enables you to
handle a given string as the name of a specified `enum` type. You must use the `class` metadata
property when specifying the Java class name of the enum, for example:
`{gender: "Female" as Enum {class: "org.mycompany.Gender"}}`

## Examples

The following examples show uses of the Java format.

- [Example: Access Values of Java Properties](#example1)

- [Example: Create a Customer Object](#example2)

- [Example: Use a Generic](#example3)

### Class Definitions

The Java examples use the following class definitions:

- [`User`](#class_user)

- [`Customer`](#class_customer) (which extends `User`)

User class:

```java
class User {
    private String name;
    private String lastName;

    public User(){

    }

    public User(String name, String lastName){
        this.name = name;
        this.lastName = lastName;
    }

    public void setName(String name){
        this.name = name;
    }

     public void setLastName(String lastName){
        this.lastName = lastName;
    }

    public String getName(){
        return name;
    }

    public String getLastName(){
        return lastName;
    }
}
```

The following class extends `User`.

Customer class:

```java
import java.util.Calendar;
class Customer extends User {
    private Calendar expirationDate;
    private User salesRepr;

    public User(){

    }

    public User(String name, String lastName,Calendar expirationDate){
        super(name,lastName);
        this.expirationDate = expirationDate;
    }

    public void setSalesRepr(User salesRepr){
        this.salesRepr = salesRepr;
    }

    public User getSalesRepr(){
        return this.salesRepr;
    }

    public void  setExpirationDate(Calendar expirationDate){
        this.expirationDate = expirationDate;
    }

    public Calendar getExpirationDate(){
        return this.expirationDate;
    }
}
```

### Example: Access Values of Java Properties

This example shows how to access the values of Java properties.

#### Input

The `User` values serves as the input payload to the DataWeave script.

```groovy
new User("Leandro", "Shokida")
```

#### Source

The DataWeave scripts transforms the input value to JSON.

```dataweave
output application/json
---
{
    a: payload.name,
    b: payload.lastName
}
```

#### Output

The output is a JSON object that contains key-value pairs. The values are the `name` and `lastName`
values from the input `User` object.

```json
{
  "a": "Leandro",
  "b": "Shokida"
}
```

### Example: Create a Customer Object

This example shows how to create an instance of a `Customer` class. The script outputs the object in
the JSON format and MIME type.

Notice that it is not necessary to specify the class of inner properties because their class is
inferred from the parent class definition.

```dataweave
output application/json
---
{
    name: "Tomo",
    lastName: "Chibana",
    expirationDate: now(),
    salesRepr: {
        name: "Mariano",
        lastName: "de Achaval",
    }
} as Object {class: "Customer"}
```

### Example: Use a Generic

This example relies on generic support in the class name to create a `java.util.ArrayList` of `User`
objects.

Note that you do not need to use a generic to specify the class in each instance. The class is taken
from the generic in the list.

```dataweave
output application/json
---
[{
    name: "Tomo",
    lastName: "Chibana"
},
{
    name: "Ana",
    lastName: "Felissati"
},
{
    name: "Leandro",
    lastName: "Shokida"
}
] as Array {class: "java.util.ArrayList<User>"}
```

## Configuration Properties

DataWeave supports the following configuration properties for this format.

### Reader Properties

There are no reader properties for this format.

### Writer Properties

This format accepts properties that provide instructions for writing output data.

<table><colgroup><col> <col> <col> <col></colgroup><thead><tr><th>Parameter</th><th>Type</th><th>Default</th><th>Description</th></tr></thead><tbody><tr><td><p><code>duplicateKeyAsArray</code></p></td><td><p><code>Boolean</code></p></td><td><p><code>false</code></p></td><td><div><div><p>Converts the values of duplicate keys in an object to a single array of values to the duplicated key.</p></div><div><p>Valid values are <code>true</code> or <code>false</code>.</p></div></div></td></tr><tr><td><p><code>writeAttributes</code></p></td><td><p><code>Boolean</code></p></td><td><p><code>false</code></p></td><td><div><div><p>Converts attributes of a key into child key-value pairs of that key. The attribute key name starts with @.</p></div><div><p>Valid values are <code>true</code> or <code>false</code>.</p></div></div></td></tr></tbody></table>

## Supported MIME Types

This format supports the following MIME types.

<table><colgroup><col></colgroup><thead><tr><th>MIME Type</th></tr></thead><tbody><tr><td><p><code>*/java</code></p></td></tr></tbody></table>
