'Check if JSON is valid in JAVA using Jackson

I have a JSON string which I am storing it in DB as a string. In front-end, I am rendering this JSON as object.

I am using:

JSON.parse(string);  
Uncaught Syntax error: Unexpected Token  

String :

{
"id": "295cd59f-4033-438c-9bf4-c571829f134e",
"from": "Shrisha S.<[email protected]>",
"to": [
    "Katie Porter <[email protected]>"
],
"cc": [
    "Jack d<[email protected]>,     Keerthi<[email protected]>"
],
"bcc": [

 ]  
}

Is there any way I can check If JSON is valid or not in JAVA?

One thing to be noted here is that, I don't have a schema defined for JSON which I can map to, i.e. JSON can hold anything.

I am currently trying out with JACKSON but for that I need a pre-defined schema which I don't have. Is there anyway this can be fixed?



Solution 1:[1]

You can read it as a JsonNode, no need to map it to a specific Class, its generic:

try{

  ObjectMapper objectMapper = ...;
  JsonNode jsonNode = objectMapper.readTree(yourJsonString);

} catch(JsonProcessingException e){........}

Solution 2:[2]

There are two different parts to the question. First is whether it is valid JSON, and second whether it contains specific set of information. @pdem already answered first part (you can also read JSON as java.lang.Object to get the same effect).

But for second part, JSON Schema is not usually a good way, as it focuses on JSON aspects but not on more meaningful part of actual data, possible sub-typing and so on, which matter at Java level where all actual data processing occurs.

So usually you would define a POJO (or ideally just use one you use for actual data processing), bind to it (with ObjectMapper.readValue()), and then check whether data is not only technically valid wrt low-level data types, but also that it conforms to additional business constraints.

For latter part you can either write Java code, or use an annotation based framework such as Bean Validation API (JSR-303); see for example:

http://beanvalidation.org/

plus there are many #bean-validation tagged questions here as well related to usage. Some frameworks add explicit support for it; for example the best Java service framework, DropWizard does this. Others like Spring Boot have support as well.

Solution 3:[3]

JSON specification forbids it from using newline characters, make sure you are replacing newline characters see Regex replace all newline characters with comma

make sure you do this before storing it in DB.

Solution 4:[4]

public boolean isValidJson(final String json) {
    try {
        final ObjectMapper objectMapper = new ObjectMapper();
        final JsonNode jsonNode = objectMapper.readTree(json);
        return jsonNode instanceof ContainerNode;
    } catch (JsonProcessingException jpe) {
        return false;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhijeet
Solution 2 StaxMan
Solution 3 Community
Solution 4 user8681709