Java Jackson wont format JSON correctly

Issue

I’m working on a little project. In short I want to generate a JSON via serialization from a list but its not formatting correctly maybe.

This is the main code for reference. The 3 other class just contains strings and one of them a enum.

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {

        List<Comment> comments  new ArrayList<>();

        Comment comm  new Comment();
                comm.setId("1");
                comm.setUser("Zoli");
                comm.setDate("2021/09/28");
                comm.setDescription("Alma");

        comments.add(comm);

        Post post  new Post();
             post.setId(1);
             post.setTitle("Alma");
             post.setUser("Soviet");
             post.setDate("2021/09/28");
             post.setStatus(Status.IN_PROGRESS);
             post.setDescription("Sample Post");
             post.setComments(comments);


        ObjectMapper objectMapper  new ObjectMapper();
        //objectMapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
        //objectMapper.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, false);
        //objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        DefaultPrettyPrinter printer  new DefaultPrettyPrinter();
        printer.indentArraysWith(new DefaultIndenter());

        String serialized  objectMapper
                .writerWithDefaultPrettyPrinter()
                .writeValueAsString(post);


        objectMapper.writeValue(new File("car.json"), serialized);

    }
}

The output from this code:

"{\r\n  \"id\" : 1,\r\n  \"title\" : \"Alma\",\r\n  \"user\" : \"Soviet\",\r\n  \"date\" : \"2021/09/28\",\r\n  \"status\" : \"IN_PROGRESS\",\r\n  \"description\" : \"Sample Post\",\r\n  \"comments\" : [Comment(id1, userZoli, date2021/09/28, descriptionAlma)]\r\n}"

What I’m looking for:

{
  "posts" : [
    { "id": 1,
      "title": "Example",
      "user": "Ruben",
      "date":  "22/09/2021",
      "status":  "done",
      "description": "Sample post",
      "comments": [
        { "id": 1, "user": "Zoli", "date": "22/09/2021", "description": "Very nice!"},
        { "id": 2, "user": "Krisz", "date": "22/09/2021", "description": "Nice!"},
        { "id": 3, "user": "Csaba", "date": "22/09/2021", "description": "Very good!"},
        { "id": 4, "user": "Márk", "date": "22/09/2021", "description": "Good!"}
      ]
    }

The things what’s commented out is the tries for solve this issue.

Solution

You were using the ObjectMapper in a wrong and too complicated way.

Instead of

DefaultPrettyPrinter printer  new DefaultPrettyPrinter();
printer.indentArraysWith(new DefaultIndenter());

String serialized  objectMapper
        .writerWithDefaultPrettyPrinter()
        .writeValueAsString(post);

objectMapper.writeValue(new File("car.json"), serialized);

you need to simply use

objectMapper.writerWithDefaultPrettyPrinter()
        .writeValue(new File("car.json"), post);

Then the output will be something like this:

{
  "id" : 1,
  "title" : "Alma",
  "user" : "Soviet",
  "date" : "2021/09/28",
  "status" : "IN_PROGRESS",
  "description" : "Sample Post",
  "comments" : [ {
    "id" : "1",
    "user" : "Zoli",
    "date" : "2021/09/28",
    "description" : "Alma"
  } ]
}

Answered By – Thomas Fritsch

Leave a Comment