2

I have a JSON file being parsed in java. It has several lists of objects like the following:

{
    "phoneNumbers": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}

It's a sensitive input file and overwriting duplicate entries is not an option. The program checks for duplicates using a for loop and maps when processing a particular list, but I want to check the entire file for dups in one go as the file is being parsed so that in case of error the program stops before running anything else.

I was wondering if there's a JSON parser for java out there that will detect duplicate object entries in the list and provide a message. Also, are there any parsers that will let you know if a declared object gets overwritten because another object with the same name is declared in the same file?

1 Answer 1

1

Well, Jackson does have a deserialization feature in its data binding extension that will do what you need: FAIL_ON_READING_DUP_TREE_KEY will cause the parser to fail on reading a duplicate key.

That said, you can probably implement the same functionality for any parser that supports deserializing into custom objects by using a custom Map class that throws an exception if a duplicate key is encountered, rather than just update the value...

As for finding duplicate entries in the list, how about deserializing into a custom LinkedHashSet subclass that throws an exception if a duplicate entry is inserted?

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

1 Comment

Thanks this helps! For checking duplicates in arrays I found a json schema validation keyword called uniqueItems, and it seems to work properly.

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.