skip to Main Content

JSON array:

{
  "ID : "ID1",
  "books" : [ {
     "bookID" : "book1",
     "booktype": "pdf"
   },{
     "bookID" : "book2",
     "booktype": "txt"
   },{
     "bookID" : "book1",
     "booktype": "txt"
  }
 ]
}

How to find bookID is duplicated or not here using Java. The JSON array will be big and contains multiple book elements. How to find if any bookID is duplicated?

Expecting an answer in Java 8 using streams or Java.

2

Answers


  1. You can learn more about using Jackson to parse JSON here. Or you can parse them to object and use stream to check for duplicates.

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class Main {
    
        public static void main(String[] args) throws JsonProcessingException {
            String json = "{ "ID" : "ID1", "books" : [ { "bookID" : "book1", "booktype": "pdf" },{ "bookID" : "book2", "booktype": "txt" },{ "bookID" : "book1", "booktype": "txt" } ] }";
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode = objectMapper.readTree(json);
            JsonNode booksNode = rootNode.get("books");
    
            Set<String> bookIds = new HashSet<>();
            booksNode.forEach(bookNode -> {
                String bookId = bookNode.get("bookID").asText();
                if (bookIds.contains(bookId)) {
                    System.out.println("Duplicate bookID found: " + bookId);
                } else {
                    bookIds.add(bookId);
                }
            });
        }
    }
    
    Login or Signup to reply.
  2. Identify duplicate values for particular element in a JSON array:

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    public class BookIdFind {
        public static void main(String[] args) throws JSONException {
            String jsonString = "{ "ID" : "ID1", "books" : [ { "bookID" : "book1", "booktype": "pdf" },{ "bookID" : "book2", "booktype": "txt" },{ "bookID" : "book1", "booktype": "txt" } ] }";
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray bookArray = jsonObject.getJSONArray("books");
            
            Map<String, List<JSONObject>> booksById = IntStream.range(0, bookArray.length())
                    .mapToObj(bookArray::getJSONObject)
                    .collect(Collectors.groupingBy(book -> book.getString("bookID")));
            
            List<String> duplicateIds = booksById.values().stream()
                    .filter(list -> list.size() > 1)
                    .map(list -> list.get(0).getString("bookID"))
                    .collect(Collectors.toList());
            
            if (duplicateIds.isEmpty()) {
                System.out.println("No duplicate book IDs found.");
            } else {
                System.out.println("Duplicate book IDs found: " + duplicateIds);
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search