0

I'm writing to write a small miroservice to read a json file and put it into postgredb. Currently it is working fine. But now i want to modify my json file with array. previously 'result' and 'HD' were not there.

{
"result": {
  "HD": [
  {
    "LOGADDR": "1000002624",
    "CATEGORY": "ADD",
    "NAME": "POLYTEC COMPOSITES GERMANY",
    "STATUS": "ACTI"
   }
  ]
 }
}

my below code is working is fine without result and HD

CommandLineRunner runner(ResponseService responseService) {
    return args -> {
        // read JSON and load json
        ObjectMapper mapper = new ObjectMapper();
        TypeReference<List<Response>> typeReference1 = new TypeReference<List<Response>>() {};
        InputStream inputStream1 = TypeReference.class.getResourceAsStream("/json/response.json");
        try {
            List<Response> responses = mapper.readValue(inputStream1, typeReference1);
            responseService.save(responses);
            System.out.println(responses);
            System.out.println("response saved");
        } catch (IOException e) {
            System.out.println("not saved" + e.getMessage());
        }

    };
}

my domain file

@AllArgsConstructor
@Data
@Entity
@Table(name = "receive")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {
@Id
@JsonProperty("LOGADDR")
private String LOGADDR;

@JsonProperty("CATEGORY")
@Column(name = "CATEGORY")
private String CATEGORY;

@JsonProperty("NAME")
@Column(name = "NAME")
private String NAME;

@JsonProperty("STATUS")
@Column(name = "STATUS")
private String STATUS;

public Response() {

}
}

1 Answer 1

1

You can update your domain object tree like your JSON :

    @Getter
    class Foo {
        Result result;
    }

    @Getter
    class Result {
        List<Response> hd;
    }

    @Getter
    class Response {
       ...
    }

    ObjectMapper mapper = new ObjectMapper();
    TypeReference<Foo> typeReference1=new TypeReference<Foo>(){};

    InputStream inputStream1 = TypeReference.class.getResourceAsStream("/json/response.json");try
    {
        Foo foo = mapper.readValue(inputStream1, typeReference1);
        List<Response> responses = foo.getResult().getHd();
        responseService.save(responses);
        System.out.println(responses);
        System.out.println("response saved");
   ...
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much! it says Entities must not be null now
in Result class getHD() is returning null. Am doing something wrong ?
` @Getter class Result {; @JsonProperty("HD") List<Response> hd = new ArrayList<>(); } `
Thank you very much! this is exactly what i was looking for! Just out of curiosity i just wanted to know what happens if i keep receiving more arrays more HD1, HD2.....HDN how to read those if in case i dont know how many arrays they are sending inside result

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.