1

I use Java Spring in my project.

I need to parse JSON into java class.

Example of the JSON:

[
  {
    "Id":"aaa1"
    "Data1":"test11"
    "Data2":"test12"
    "url1": "https://...someimg11.png",
    "url2": "https://...someimg12.png",
  },
  {
    "Id":"aaa2"
    "Data1":"test21"
    "Data2":"test22"
    "url1": "https://...someimg21.png",
    "url2": "https://...someimg22.png",
  },
  {
    "Id":"aaa3"
    "data1":"test31"
    "data2":"test32"
    "url1": "https://...someimg31.png",
    "url2": "https://...someimg32.png",
  }
 ]
 

And here the Java class that json above should be parsed to:

  class Info{
    @JsonProperty("Id")
    public String id;
    
    @JsonProperty("Data1")
    public String data1;
    
    @JsonProperty("Data2")
    public String data2;
    
    //parse to here url1 and url2 properties from json doc
    public List<String> urls;
 }
 

As you can see I have no issues to parse properties, except the last properties in JSON url1 and url2, the properties of JSON file url1 and url2 should be parsed into URLs property of type List.

My question is, how can I parse the two properties of JSON inside the single property in the class of type list of strings?

UPDATE I can use @JsonAnySetter annotation as suggested on one of the answers, but in this case, I will need to initialize the urls property, otherwise, I get this error on parsing:

Cannot invoke "java.util.List.add(Object)" because "this.urls" is null

In case if I use @JsonAnySetter annotation how can I init urls property?

3
  • It is better to use tow objects, entity and DTO objects. Commented Mar 1, 2021 at 17:57
  • Use JsonAnySetter annotation and create collection directly in class before annotated method will be invoked. See similar solution: Map JSON key-value to class in java Commented Mar 1, 2021 at 20:03
  • Better way, to have urls as array in JSON object. In future, if you have to add another url then it will need code changes as well. Commented Mar 1, 2021 at 20:21

1 Answer 1

1

You can do it using @JsonAnySetter annotation and I don't if there is any easier way of using patterns or wildcards

class Info{

    @JsonProperty("Id")
    public String id;

    @JsonProperty("Data1")
    public String data1;

    @JsonProperty("Data2")
    public String data2;

     //parse to here url1 and url2 properties from json doc
     public List<String> urls = new ArrayList<>();

     @JsonAnySetter
     public void add(String key, String value) {
      if(key.equals("url1") || key.equals("url2")) {
           urls.add(value);
        }
    }
  }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the post. In case if I use JsonAnySetter annotation how can I initialize urls vale? Otherwise, it will have a null value.
Updated the answer @Michael
See my update you just initialize the property at the time of declaration @Michael

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.