0

I have written this below code to read JSON and save in PostgreSQL, but it does not work when it is nested, only the last two records are added. How can I loop nested arrray HD. There 2 nested arrays HD in this case. How can I make this work? should I have to loop HD array or am I doing something wrong here?

{
 "result": {
 "HD": [
  {
    "ADDR": "2218",
    "CAT": "s",
    "NAME": "Last"
  }
  ],
  "HD": [
  {
    "ADDR": "2219",
    "CAT": "w",
    "NAME": "Last"
  },
  "HD":
  {
    "ADDR": "2220",
    "CAT": "m",
    "NAME": "Last"
  }
  ]
}

My domain classes

@AllArgsConstructor
@Data
@Entity
@Table(name ="receive", schema = "public")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {

    @Id
    @JsonProperty("ADDR")
    @Column(name = "addr")
    private String ADDR;

    @JsonProperty("CAT")
    @Column(name = "cat")
    private String CAT;

    @JsonProperty("NAME")
    @Column(name = "name")
    private String NAME;
}
@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Result {
    @JsonProperty("HD")
    List<Response> HD = new ArrayList<>();
    }

    @Getter
    public class AddressArray {
    Result result;
}

This is my main class; I'm pretty sure I'm missing something here

public class ReceiveApplication {

    public static void main(String[] args) {
        SpringApplication.run(ReceiveApplication.class, args);
    }

    @Bean
    CommandLineRunner runner(ResponseService responseService) {
        return args -> {
            ObjectMapper mapper = new ObjectMapper();
            TypeReference<AddressArray> typeReference1 = new TypeReference<AddressArray>() {
            };
            InputStream inputStream = TypeReference.class.getResourceAsStream("/json/response.json");
            try {
                AddressArray addressArray = mapper.readValue(inputStream, typeReference1);
                List<Response> responses = addressArray.getResult().getHD();
                responseService.save(responses);
                System.out.println(responses);
                System.out.println("Data saved in table");

            } catch (IOException e) {
                System.out.println("not saved " + e.getMessage());

            }

        };
    }
}

1 Answer 1

2

The problem is within the JSON, you have two HD entries there. First, this one:

"HD": [
  {
    "ADDR": "2218", ...
  }
]

and then a second one, at the same level:

"HD": [
  {
    "ADDR": "2219", ...
  },
  "HD":
  {
    "ADDR": "2220", ...
  }
]

Imagine this is read as a key-value-map, with "HD" being the key. During parsing, the first entry gets stored in the map, but it gets overwritten by the second entry, which has the same key. So effectively, the first entry is lost and does not get stored.
You did not mention where the input comes from, but if you are able to modify it, you should simply put all HD values in one array:

"HD": [
  {
    "ADDR": "2218", ...
  },
  {
    "ADDR": "2219", ...
  },
  {
    "ADDR": "2220", ...
  }
]

Update:
If changing the input is not an option, you could read the data in a more generic way as described here.
And to answer your other question, for reading attributes named HD,HD1,HD2 etc. you would simply put multiple members in your response class:

List<Response> HD = new ArrayList<>();
List<Response> HD1 = new ArrayList<>();
List<Response> HD2 = new ArrayList<>();

You get the picture ;-)

Sign up to request clarification or add additional context in comments.

6 Comments

it is coming from a server.. How read the json if its HD HD1 HD2 how can i loop array
Ok, that's unfortunate. In that case you need to parse the data in a more generic fashion, maybe this is helpful: stackoverflow.com/a/15888592/8668332
Thank you so much!! just out of curiosity how can i read the json if its HD HD1 HD2
That's quite easy, I added an update to the answer above. If it helped, it would be kind of you to mark it as the accepted answer.
No worries, we've all been there :D If you want to concatenate all those responses, you could take the first list and add the other ones to it: List<Response> responses = addressArray.getResult().getHD(); responses.addAll(addressArray.getResult().getHD1()); and the save the responses list.
|

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.