Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get values from Hashmap using JSON data?

InputStream getLocalJsonFile = new FileInputStream("C:\\Users\\LENEVO\\IdeaProjects\\Coding\\src\\main\\resources\\EU.json");

        List<Map<String,Object>> jsonMap = new ObjectMapper().readValue(getLocalJsonFile,new TypeReference<List<Map<String, Object>>>(){});
        System.out.println(jsonMap);

I could get all the values on hashmap. But I want to check if sbi is there in default bank and if so to display only sbi’s ifsc and branch name. But not sure how to display that. Please refer to the below json data for reference.

[

  {
    "DefaultBank": "sbi",
    "IFSC": "1231",
    "BRANCH": "abc"
  },
{
    "DefaultBank": "icici",
    "IFSC": "566",
    "BRANCH": "sdf"
  }

]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Just filter all object inside the list and find first which matches

ObjectMapper mapper = new ObjectMapper();
    List<Map<String, Object>> jsonMap = mapper.readValue(getLocalJsonFile, new TypeReference<List<Map<String, Object>>>() {
    });

    Optional<Map<String, Object>> first = jsonMap.stream().filter(map -> map.get("DefaultBank") instanceof String && "sbi".equalsIgnoreCase(map.get("DefaultBank").toString())).findFirst();
    first.ifPresent((defaultBank) -> {
        System.out.println(defaultBank.get("IFSC"));
        System.out.println(defaultBank.get("BRANCH"));
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading