Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON many objects to single JSON using Jackson

Tags:

java

json

jackson

I have JSON, with differents levels field, so I want to convert to a single JSON with fields with one level for example:

{
  "prop1":"value1",
  "prob2":"value2",
  "prop3": {
     "prop4":"value4",
     "prop5":"value5"
  }
  ... many level fields
}  

result

{
  "prop1":"value1",
  "prop2":"value2",
  "prop4":"value4",
  "prop5":"value5"
  .......
}

I'm using Jackson with annotation @JsonProperty("field"), I haven't problem wih fields of first level , but I don´t know how to access field where to into more inside JSON , for this example are prop4 and prop5.

like image 737
cflores29 Avatar asked May 20 '13 16:05

cflores29


People also ask

How do I convert JSON objects to Jackson?

Converting Java object to JSON In it, create an object of the POJO class, set required values to it using the setter methods. Instantiate the ObjectMapper class. Invoke the writeValueAsString() method by passing the above created POJO object. Retrieve and print the obtained JSON.

Is GSON better than Jackson?

ConclusionBoth Gson and Jackson are good options for serializing/deserializing JSON data, simple to use and well documented. Advantages of Gson: Simplicity of toJson/fromJson in the simple cases. For deserialization, do not need access to the Java entities.

What is the use of JsonAlias?

The @JsonAlias annotation can define one or more alternate names for the attributes accepted during the deserialization, setting the JSON data to a Java object. But when serializing, i.e. getting JSON from a Java object, only the actual logical property name is used instead of the alias.


1 Answers

JsonUnwrapped is the annotation to use, it even works for multi-level nesting. For example:

@RunWith(JUnit4.class)
public class Sample {

    @Test
    public void testName() throws Exception {
        SampleClass sample = new SampleClass("value1", "value2", new SubClass("value4", "value5", new SubSubClass("value7")));
        new ObjectMapper().writeValue(System.out, sample);
    }

    @JsonAutoDetect(fieldVisibility=Visibility.ANY)
    public static class SampleClass {
        private String prop1;
        private String prop2;
        @JsonUnwrapped
        private SubClass prop3;

        public SampleClass(String prop1, String prop2, SubClass prop3) {
            this.prop1 = prop1;
            this.prop2 = prop2;
            this.prop3 = prop3;
        }
    }
    @JsonAutoDetect(fieldVisibility=Visibility.ANY)
    public static class SubClass {
        private String prop4;
        private String prop5;
        @JsonUnwrapped
        private SubSubClass prop6;
        public SubClass(String prop4, String prop5, SubSubClass prop6) {
            this.prop4 = prop4;
            this.prop5 = prop5;
            this.prop6 = prop6;
        }

    }
    @JsonAutoDetect(fieldVisibility=Visibility.ANY)
    public static class SubSubClass{
        private String prop7;

        public SubSubClass(String prop7) {
            this.prop7 = prop7;
        }
    }
}

will generate

{"prop1":"value1","prop2":"value2","prop4":"value4","prop5":"value5","prop7":"value7"}
like image 161
Pascal Gélinas Avatar answered Oct 14 '22 10:10

Pascal Gélinas