Bird
Raised Fist0
Spring Bootframework~20 mins

JSON serialization with Jackson in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Jackson JSON Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the JSON output of this Spring Boot controller method?

Consider a Spring Boot REST controller method that returns a simple POJO. What JSON will be sent to the client?

Spring Boot
public record User(String name, int age) {}

@GetMapping("/user")
public User getUser() {
    return new User("Alice", 30);
}
A{"user":{"name":"Alice","age":30}}
B{"name":"Alice","age":"30"}
C{"Name":"Alice","Age":30}
D{"name":"Alice","age":30}
Attempts:
2 left
💡 Hint

Jackson uses field names as JSON keys by default and serializes numbers as numbers.

📝 Syntax
intermediate
2:00remaining
Which option correctly configures Jackson to ignore null fields in JSON output?

You want to configure Jackson in Spring Boot to skip null fields when serializing objects. Which code snippet achieves this?

A
@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper().disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
}
B
@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
C
@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.ALWAYS);
}
D
@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper().enable(SerializationFeature.WRITE_NULL_MAP_VALUES);
}
Attempts:
2 left
💡 Hint

Jackson's setSerializationInclusion controls which fields are included.

🔧 Debug
advanced
2:00remaining
Why does this Jackson serialization produce an empty JSON object?

Given this class and controller, why does the JSON output show {} instead of the expected fields?

Spring Boot
public class Product {
    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() { return name; }
    public double getPrice() { return price; }
}

@GetMapping("/product")
public Product getProduct() {
    return new Product("Book", 12.99);
}
ABecause the fields are private and there are no getters, Jackson cannot access them.
BBecause the class is missing the @JsonSerializable annotation.
CBecause the price field is a primitive type and cannot be serialized.
DBecause the controller method does not return ResponseEntity.
Attempts:
2 left
💡 Hint

Jackson needs a way to read field values, usually via getters or public fields.

🧠 Conceptual
advanced
2:00remaining
What is the effect of using @JsonIgnoreProperties on a class?

In Jackson, what does adding @JsonIgnoreProperties({"field1", "field2"}) to a class do?

AIt renames the specified fields in the JSON output.
BIt marks the specified fields as required during deserialization.
CIt prevents the specified fields from being included in JSON serialization and deserialization.
DIt makes the specified fields transient and skips them only during serialization.
Attempts:
2 left
💡 Hint

Think about ignoring fields in both directions: reading and writing JSON.

❓ state_output
expert
3:00remaining
What JSON output results from this custom serializer in Spring Boot?

Given this custom serializer registered for a class, what JSON will be produced?

Spring Boot
public class Color {
    private int red, green, blue;
    public Color(int r, int g, int b) { red = r; green = g; blue = b; }
    public int getRed() { return red; }
    public int getGreen() { return green; }
    public int getBlue() { return blue; }
}

public class ColorSerializer extends StdSerializer<Color> {
    public ColorSerializer() { super(Color.class); }
    @Override
    public void serialize(Color value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        String hex = String.format("#%02X%02X%02X", value.getRed(), value.getGreen(), value.getBlue());
        gen.writeString(hex);
    }
}

// Registered in ObjectMapper

@GetMapping("/color")
public Color getColor() {
    return new Color(255, 165, 0);
}
A"#FFA500"
B{"red":255,"green":165,"blue":0}
C"rgb(255,165,0)"
D{"color":"#FFA500"}
Attempts:
2 left
💡 Hint

The custom serializer writes a string with the hex color code.

Practice

(1/5)
1. What is the primary purpose of Jackson in a Spring Boot application?
easy
A. To convert Java objects to JSON and vice versa
B. To manage database connections
C. To handle HTTP requests
D. To style HTML pages

Solution

  1. Step 1: Understand Jackson's role

    Jackson is a library used to convert Java objects into JSON format and back.
  2. Step 2: Compare options with Jackson's function

    Only To convert Java objects to JSON and vice versa describes this conversion role; others relate to different tasks.
  3. Final Answer:

    To convert Java objects to JSON and vice versa -> Option A
  4. Quick Check:

    Jackson = Java-JSON conversion [OK]
Hint: Jackson is for JSON conversion, not database or UI tasks [OK]
Common Mistakes:
  • Confusing Jackson with database or HTTP handling
  • Thinking Jackson styles HTML
  • Assuming Jackson manages server requests
2. Which of the following is the correct way to serialize a Java object to a JSON string using Jackson's ObjectMapper?
easy
A. objectMapper.readValue(object, String.class);
B. objectMapper.writeValueAsString(object);
C. objectMapper.toJson(object);
D. objectMapper.serialize(object);

Solution

  1. Step 1: Identify the serialization method in ObjectMapper

    The method to convert an object to JSON string is writeValueAsString.
  2. Step 2: Check other options for correctness

    readValue is for deserialization, toJson and serialize are not valid ObjectMapper methods.
  3. Final Answer:

    objectMapper.writeValueAsString(object); -> Option B
  4. Quick Check:

    Serialize with writeValueAsString() [OK]
Hint: Use writeValueAsString() to convert object to JSON string [OK]
Common Mistakes:
  • Using readValue() instead of writeValueAsString()
  • Assuming toJson() or serialize() exist in ObjectMapper
  • Mixing serialization and deserialization methods
3. Given the following code snippet, what will be the output?
ObjectMapper mapper = new ObjectMapper();
User user = new User("Alice", 30);
String json = mapper.writeValueAsString(user);
System.out.println(json);
Assuming User class has fields 'name' and 'age' with getters.
medium
A. {"Alice":30}
B. {"userName":"Alice","userAge":30}
C. {"name":"Alice","age":30}
D. Error: Cannot serialize User object

Solution

  1. Step 1: Understand default serialization of User object

    Jackson serializes fields with their names and values, so 'name' and 'age' become JSON keys and values.
  2. Step 2: Match output with expected JSON format

    {"name":"Alice","age":30} matches the expected JSON string with keys 'name' and 'age' and their values.
  3. Final Answer:

    {"name":"Alice","age":30} -> Option C
  4. Quick Check:

    Default serialization = field names as keys [OK]
Hint: Default JSON keys match Java field names with getters [OK]
Common Mistakes:
  • Expecting custom key names without annotations
  • Assuming serialization fails without extra config
  • Confusing field names with variable names
4. What is wrong with this code snippet that tries to pretty-print JSON?
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.PRETTY_PRINT);
String json = mapper.writeValueAsString(user);
medium
A. SerializationFeature.PRETTY_PRINT does not exist; should use INDENT_OUTPUT
B. writeValueAsString cannot produce pretty JSON
C. ObjectMapper must be configured with a custom serializer
D. Pretty print requires calling writeValue() instead

Solution

  1. Step 1: Check correct SerializationFeature for pretty print

    The correct feature name is INDENT_OUTPUT, not PRETTY_PRINT.
  2. Step 2: Verify usage of writeValueAsString

    writeValueAsString supports pretty printing if INDENT_OUTPUT is enabled.
  3. Final Answer:

    SerializationFeature.PRETTY_PRINT does not exist; should use INDENT_OUTPUT -> Option A
  4. Quick Check:

    Use INDENT_OUTPUT for pretty print [OK]
Hint: Use SerializationFeature.INDENT_OUTPUT for pretty JSON [OK]
Common Mistakes:
  • Using wrong feature name PRETTY_PRINT
  • Thinking writeValueAsString can't pretty print
  • Believing custom serializers are always needed
5. You want to serialize a Java object to JSON but exclude null fields from the output. Which Jackson configuration achieves this?
hard
A. mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
B. mapper.disable(SerializationFeature.WRITE_NULLS);
C. mapper.enable(SerializationFeature.EXCLUDE_NULLS);
D. mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

Solution

  1. Step 1: Identify correct method to exclude null fields

    Jackson uses setSerializationInclusion with JsonInclude.Include.NON_NULL to skip nulls.
  2. Step 2: Check other options for validity

    WRITE_NULLS and EXCLUDE_NULLS are not valid features; ALWAYS includes all fields.
  3. Final Answer:

    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); -> Option D
  4. Quick Check:

    Exclude nulls with NON_NULL inclusion [OK]
Hint: Use NON_NULL inclusion to skip null fields [OK]
Common Mistakes:
  • Using non-existent SerializationFeature flags
  • Confusing ALWAYS with excluding nulls
  • Trying to disable null writing with wrong methods
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard