Search code examples
javajsonserializationjacksonjsonserializer

Serialize JSON document using Jackson


I am trying to serialize JSON document using Jackson library. Below is my JSON document that I have created by hand. Now I need to serialize this document using Jackson

Example-A

{
    "v" : {
        "site_id" : 0,
        "price_score" : 0.5,
        "confidence_score" : 0.2,
        "categories": {
            "123" : {
                "price_score": "0.5",
                "confidence_score": "0.2"
            },
            "321" : {
                "price_score": "0.2",
                "confidence_score": "0.4"
            }
        }
    }
}

I am able to make this part of JSON document till now with my below code and using Jackson-

Example-B

{
  "v" : {
    "site_id" : 0,
    "price_score" : 0.5,
    "confidence_score" : 0.2
  }
}       

Now, I am not able to understand how do I add the list of categories (as shown in Example-A) portion in my Example-B JSON document with my below code?

public static void main(String[] args) {

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("site-id", 0);
    props.put("price-score", 0.5);
    props.put("confidence-score", 0.2);

    AttributeValue av = new AttributeValue();
    av.setProperties(props);

    /**
     * this will print out the JSON document like I shown in my Example-B
     * but I need to make it look like as in Example-A. I am not sure how
     * to achieve that?
     */
    System.out.println(av);

    // serialize it
    try {
        String jsonStr = JsonMapperFactory.get().writeValueAsString(attr);
        System.out.println(jsonStr);
    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Can anybody help me with that?


Solution

  • Solution 1
    In you case you can do it with

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("site-id", 0);
    props.put("price-score", 0.5);
    props.put("confidence-score", 0.2);
    Map<String, String> category123 = new HashMap<String, String>();
    category123.put("price_score", "0.5");
    category123.put("confidence_score", "0.2");
    Map<String, String> category321 = new HashMap<String, String>();
    category123.put("price_score", "0.2");
    category123.put("confidence_score", "0.4");
    Map<String, Object> categories = new HashMap<String, Object>();
    categories.put("123", category123);
    categories.put("321", category321);
    props.put("categories", categories);  
    

    Solution 2:
    Or you can simplify it with additional classes, e.g.:

    public class Category 
    {
       private String price_score;
       private String confidence_score;
    
       public Category(String price_score, String confidence_score) 
       {
          this.price_score = price_score;
          this.confidence_score = confidence_score;
       }
    
       public Category() 
       {
       } 
       // getters/setters
    }
    

    main method

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("site-id", 0);
    props.put("price-score", 0.5);
    props.put("confidence-score", 0.2);
    Map<String, Category> categories = new HashMap<String, Category>();
    categories.put("123", new Category("0.4", "0.2"));
    categories.put("321", new Category("0.2", "0.5"));
    props.put("categories", categories);