Unrecognized field "X" (class Y), not marked as ignorable

ErrorJava

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "carrier" (class com.example.Order), not marked as ignorable (2 known properties: "status", "id")

What it means

The JSON has a key, carrier, that your class does not have, and a plain Jackson 2 ObjectMapper fails on unknown keys. Add @JsonIgnoreProperties(ignoreUnknown = true) to the class, or add the field if you need it.

Last verified on Jackson 2.21 and 3.1; Spring 6.2 and 7.0.

This JSON is valid. The problem is on the Java side, so a JSON validator would say it is fine.

Usually
The sender added a fieldA misspelled or renamed keyIt works in Spring but fails in your test
Fix
Add @JsonIgnoreProperties(ignoreUnknown = true) to the class, as in the fixed code.

The fix

Add @JsonIgnoreProperties(ignoreUnknown = true) to the class, as in the fixed code. If the key was renamed, add or rename the field instead.

The JSON that was sent: request.jsonValid JSON
{
  "id": 7,
  "status": "shipped",
  "carrier": "DHL"
}
A JSON validator says this is fine.
Broken.javaThrows
public class Order {
  public long id;
  public String status;
}

Order order = mapper.readValue(json, Order.class);
Throws UnrecognizedPropertyException

Line 1 changes.

Fixed.javaWorks
@JsonIgnoreProperties(ignoreUnknown = true)
public class Order {
  public long id;
  public String status;
}

Order order = mapper.readValue(json, Order.class);
Reads without an error.

How the message appears

Only a plain Jackson 2 ObjectMapper throws this, so there is one error and where it stopped. Jackson 3 and Spring 6.2 and 7.0 read the same JSON and skip the unknown key.

Console
  • com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "carrier" (class com.example.Order), not marked as ignorable (2 known properties: "status", "id")
  • at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 4, column: 15] (through reference chain: com.example.Order["carrier"])

What causes it

  1. The sender added a field

    The other side added a field and your class has not caught up. The message tells you both sides: the key it did not know, "carrier", and the ones it does, (2 known properties: "status", "id").

  2. A misspelled or renamed key

    If the JSON says carrier and your field is shipper, Jackson cannot match them. Compare the unknown key with the known properties in the message.

  3. It works in Spring but fails in your test

    Jackson 3 and Spring 6.2 and 7.0 read this same JSON and skipped carrier. Only a plain Jackson 2 ObjectMapper threw. So a test that creates its own new ObjectMapper() fails while the app works. See the Spring page.

Worth knowing

Watch outIgnoring unknown keys also hides a misspelled one. The field just stays empty and nothing tells you. Use it where the sender is allowed to add fields, not everywhere.

Your JSON and text are processed entirely in your browser and are never sent anywhere. To understand how the site is used we record each page view (the page, the referrer and an approximate location from your IP address) and keep it for 90 days. Nothing is stored on your device unless you choose it. Accept to also store a device identifier here and record your browser and device characteristics, which is what lets a return visit be told from a new one. Decline and nothing is sent at all. We respect Do Not Track. See our privacy policy.