8

I have a javax.json.JsonObject and want to validate it against a JSON schema. So I've found the com.github.fge.json-schema-validator. But it works only with com.fasterxml.jackson.databind.JsonNode.

Is there a way to convert my JsonObject into a JsonNode?

0

2 Answers 2

15
public JsonNode toJsonNode(JsonObject jsonObj) { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readTree(jsonObj.toString()); } 

this will just to it. JsonObject.toString() will convert to json String, you don't need to use anything else.

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

Comments

8

The following solution parses a javax.json.JsonObject into a JSON string and then parses the JSON string into a com.fasterxml.jackson.databind.JsonNode using Jackson's ObjectMapper:

public JsonNode toJsonNode(JsonObject jsonObject) { // Parse a JsonObject into a JSON string StringWriter stringWriter = new StringWriter(); try (JsonWriter jsonWriter = Json.createWriter(stringWriter)) { jsonWriter.writeObject(jsonObject); } String json = stringWriter.toString(); // Parse a JSON string into a JsonNode ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(json); return jsonNode; } 

7 Comments

This it is! Thank you very much :)
@frogeyedpeas Is the lack of the import details the reason for the downvote?
No I didn't downvote, i only stumbled upon this just now and it seems relevant to something I'm working on.
@frogeyedpeas I'll consider updating the answer with the import details very soon. For now, the qualified class names on the top of the answer give you a hint.
@frogeyedpeas Please refer to javax.json.Json. It's part of the Java EE 7 and was introduced in the JSR 353, the Java API for JSON Processing.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.