1

Hi I am having an json data file containing an array of objects. I am reading the json file and convert it into a list of java object . However when i use the object mapper to convert it to json document i am seeing values printed with a precision followed by zeros . any idea how can i fix it .

so following is my json data file.

[
  {
    "inventory": "SMS",
    "msg_text": "This is random text",
    "status": "ENROUTE",
    "@timestamp": "2019-09-02T03:26:26.770Z",
    "o_error": "",
    "flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
    "recipient": 420736408283,
    "account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
    "sender": 8800111,
    "submission_ts": 1567681407,
    "campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
    "nof_segments": 1,
    "@version": 1,
    "delivery_ts": 1558761537
  },
  {
    "inventory": "SMS",
    "msg_text": "This is random text",
    "status": "ENROUTE",
    "@timestamp": "2019-09-02T09:48:09.213Z",
    "o_error": "",
    "flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
    "recipient": 420736408283,
    "account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
    "sender": 8800111,
    "submission_ts": 1567681407,
    "campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
    "nof_segments": 1,
    "@version": 1,
    "delivery_ts": 1549246209
  }]

The following is the code to read the file , convert it into a json object and print .

ObjectMapper objMapper = new ObjectMapper();
objMapper.registerModule(new JavaTimeModule());          

objMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

List<MessageHistory> messageHistories = objMapper.readValue(getMessageHistoryResourceAsStream(),
                    objMapper.getTypeFactory().constructCollectionType(List.class, MessageHistory.class));
for (int i = 0; i < messageHistories.size(); i++) {
System.out.println(objMapper.writeValueAsString(messageHistories.get(i)));

}

output is below

{"inventory":"SMS","msg_text":"This is random text","status":"ENROUTE","@timestamp":"2019-09-04T17:43:03.518Z","o_error":"","flight_id":"92348fa1-ca6c-456a-b3b2-85fba2d2deed","recipient":"420736408281","account_id":"a56f7e14-20f9-40e6-90c6-10604140ac5f","sender":"8800111","campaign_id":"6f2abca3-b46d-43f3-91be-3278a8dd7dc0","nof_segments":1,"@version":1,"submission_ts":1567681407.000000000,"delivery_ts":1562941055.000000000}
{"inventory":"SMS","msg_text":"This is random text","status":"ENROUTE","@timestamp":"2019-09-01T09:05:45.540Z","o_error":"","flight_id":"92348fa1-ca6c-456a-b3b2-85fba2d2deed","recipient":"420736408283","account_id":"a56f7e14-20f9-40e6-90c6-10604140ac5f","sender":"8800111","campaign_id":"6f2abca3-b46d-43f3-91be-3278a8dd7dc0","nof_segments":1,"@version":1,"submission_ts":1567681407.000000000,"delivery_ts":1558368683.000000000}

you can see submission_ts and delivery_ts has .000000000 to it ..

so following is my MessageHistory.java file what i use to bind the json data to object.

import java.time.Instant;
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

public class MessageHistory {

    @JsonProperty("inventory")
    private String inventory;

    @JsonProperty("msg_text")
    private String messageText;

    @JsonProperty("status")
    private String status;

    @JsonProperty("@timestamp")
    private Instant timeStamp;

    @JsonProperty("o_error")
    private String error;

    @JsonProperty("flight_id")
    private UUID flightId;

    @JsonProperty("recipient")
    private String recipient;

    @JsonProperty("account_id")
    private UUID accountId;

    @JsonProperty("sender")
    private String sender;

    @JsonProperty("campaign_id")
    private UUID campaignId;

    @JsonProperty("nof_segments")
    private Integer segmentCount;

    @JsonProperty("@version")
    private Integer version;

    @JsonProperty("submission_ts")
    @JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
    private Instant submissionTimeStamp;

    @JsonProperty("delivery_ts")
    @JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
    private Instant deliveryTimeStamp;

    public String getInventory() {
        return inventory;
    }

    public void setInventory(String inventory) {
        this.inventory = inventory;
    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Instant getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(Instant timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public UUID getFlightId() {
        return flightId;
    }

    public void setFlightId(UUID flightId) {
        this.flightId = flightId;
    }

    public String getRecipient() {
        return recipient;
    }

    public void setRecipient(String recipient) {
        this.recipient = recipient;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public UUID getAccountId() {
        return accountId;
    }

    public void setAccountId(UUID accountId) {
        this.accountId = accountId;
    }

    public UUID getCampaignId() {
        return campaignId;
    }

    public void setCampaignId(UUID campaignId) {
        this.campaignId = campaignId;
    }

    public Integer getSegmentCount() {
        return segmentCount;
    }

    public void setSegmentCount(Integer segmentCount) {
        this.segmentCount = segmentCount;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Instant getSubmissionTimeStamp() {
        return submissionTimeStamp;
    }

    public void setSubmissionTimeStamp(Instant submissionTimeStamp) {
        this.submissionTimeStamp = submissionTimeStamp;
    }

    public Instant getDeliveryTimeStamp() {
        return deliveryTimeStamp;
    }

    public void setDeliveryTimeStamp(Instant deliveryTimeStamp) {
        this.deliveryTimeStamp = deliveryTimeStamp;
    }
}

I am annotating the fileds

@JsonProperty("submission_ts")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
private Instant submissionTimeStamp;

@JsonProperty("delivery_ts")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
private Instant deliveryTimeStamp;

really appreciate any help to solve why the objects when printed as json has that .000000 attached to those two fields thank you

2
  • You can try google's gson for json to java object conversion..Gson gson = new Gson(); gson.fromJson(json object, Java class name.class) Commented Sep 5, 2019 at 13:56
  • @NKR What's the advantage of using Gson instead of Jackson in this situation? Commented Sep 5, 2019 at 13:58

1 Answer 1

1

You want to disable the WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS feature.

From the documentation:

Feature that controls whether numeric timestamp values are to be written using nanosecond timestamps (enabled) or not (disabled);


You can either disable it in ObjectMapper (which applies to all fields):

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);

Or disable it at field level:

@JsonFormat(shape = Shape.NUMBER,
            without = Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
private Instant instant;

If you intend to use this configuration in multiple fields, you may want to consider @JacksonAnnotationsInside which allows you to create a custom annotation which contains one or more Jackson annotations:

@Retention(value = RUNTIME)
@JacksonAnnotationsInside
@JsonFormat(shape = Shape.NUMBER,
            without = Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
public @interface JsonTimestamp {

}

Then simply use the newly created annotation:

@JsonTimestamp
private Instant instant;
Sign up to request clarification or add additional context in comments.

1 Comment

did lots of search and finally this answer works, using the annotation @JsonFormat(shape = Shape.NUMBER, without = Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) Many thanks!

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.