Detect JSON Schema from JSON String in Java

I was looking for a way to detect a JSON object schema that comes from a JSON string in Java. What I find are many ways to convert a JSON string to POJO, but all the examples I found require the Java class to convert JSON in order to be known and defined before.

My JSON is data, and I don’t know its structure, so I can’t do this.

I also see many ways to serialize POJO for JSON, and the answer to most of these questions is to use the Jackson module, but I still haven't found the answer to my question:

I want to read a JSON string in which I do not know the structure and do not detect its scheme. is there a module for this? I could go through this and handle different cases, but this must have been done before!

I know I can read JSON with something like

mapper = new org.codehaus.jackson.map.ObjectMapper ();

JsonNode json = mapper.readTree (in);

and I can possibly read the Java class diagram with

Schema JsonSchema = mapper.generateJsonSchema (myObjectClass.class);

but if i just do

Schema JsonSchema = mapper.generateJsonSchema (json.getClass ());

all I get from this, no matter what the JSON string input is, is this:

{"type": "Object"}

So, does anyone know how I can detect the schema of my unknown object?

I assume this has to do with building the POJO correctly from JsonNode, but I'm not sure how to get this.

Thank you for understanding.

+4
source share

All Articles