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 :

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"));
    });

Leave a Reply