0

TL;DR: when my JSON array is converted to a list of Java objects, the null values in the JSON string is converted to 0 instead of staying as null.

I want to keep them as null in the Java object.


I have the following API response:

[
    {
        "ID": 1,
        "MarginUpper": null
    },
    {
        "ID": 2,
        "MarginUpper": null
    }
]

I also have the following POJO

package com.myapp.pojo;

import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;

public class Margins {
    @Getter
    @Setter
    @SerializedName("ID")
    private int id;

    @Getter
    @Setter
    @SerializedName("MarginUpper")
    private int marginUpper;
    // Constructor with all fields
    public NaplanGain(int iId, int marginUpper) {
        this.id = id;
        this.marginUpper = marginUpper;
    }
}

Using this code, I can convert the JSON string into a list of Java objects.

Type listType = new TypeToken<List<Margins>>() {}.getType();
List<Margins> list = new GsonBuilder().serializeNulls().create().fromJson(apiResponse, listType);

ID=1 will have a marginUpper=0 when I inspect the value when debugging

2
  • 10
    Primitive datatypes like int can not be null. Use Objects Integer if you want to have null values Commented Sep 8 at 6:27
  • @Jens let me try. Thanks! Commented Sep 8 at 6:29

3 Answers 3

2
package com.myapp.pojo;

import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;

public class Margins {
    @Getter
    @Setter
    @SerializedName("ID")
    private int id;

    @Getter
    @Setter
    @SerializedName("MarginUpper")
    private Integer marginUpper;
    // Constructor with all fields
    public Margins(int iId, int marginUpper) {
        this.id = id;
        this.marginUpper = marginUpper;
    }
}

You used marginUpper value as int which is Primitive and cannot be null. If you want it to be same as json. Change it to Integer.

Sign up to request clarification or add additional context in comments.

Comments

0

if marginUpper can be null, you should use Integer instead of int

public class Margins {

    private int id;

    private Integer marginUpper;

    // Getter Setter NoArgConstructor AllArgsConstructor
}

1 Comment

Why does this correct Answer deserve a Down-Vote?
-1

tl;dr

Use Integer, not int.

record Margin( int id , Integer upper ) { }

Details

The first two Answers by wjfqvi and by Abhinav Karn are both correct: Use Integer class rather than int primitive to accommodate null values. A primitive int in Java cannot be null, but an object reference can be null.

Example code

Here is some example code showing how to parse your JSON as Java record objects.

First, your sample data, modified slightly.

String json = """
        [
            {
                "id": 1,
                "upper": null
            },
            {
                "id": 2,
                "upper": 42
            }
        ]
        """;

Next, define the record class that we want instantiated while parsing our JSON.

record Margin( int id , Integer upper ) { }

Launch GSON, tell it to parse.

Gson gson = new Gson();
Margin[] margins = gson.fromJson( json , Margin[].class );

Dump to console.

Arrays.stream( margins ).forEach( IO :: println );

We find success.

Margin[id=1, upper=null]
Margin[id=2, upper=42]

I used Java 25, GSON 2.13.1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.