17

How do I create a ObjectNode from a string using Jackson?

I tried:

ObjectNode json = new ObjectMapper().readValue("{}", ObjectNode.class);

But get

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "type": jdk.nashorn.internal.ir.Symbol#setType(1 params) vs jdk.nashorn.internal.ir.Symbol#setType(1 params)

I want to be able to read a JSON string the add/modify some values.

2 Answers 2

11

You are using the wrong import.

It should be

com.fasterxml.jackson.databind.node.ObjectNode

Not:

jdk.nashorn.internal.ir.ObjectNode
Sign up to request clarification or add additional context in comments.

Comments

11

Firstly, the error message suggests you're tying to build a jdk.nashorn.internal.ir.ObjectNode, whereas I'm guessing you actually intended to build a com.fasterxml.jackson.databind.node.ObjectNode (for Jackson 2.x). Check your imports.

However, if all you want to do is build an empty ObjectNode, then just use

JsonNodeFactory.instance.objectNode()

If for some reason you really want to do it by parsing an empty JSON object, then use this:

ObjectNode json = (ObjectNode) new ObjectMapper().readTree("{}");

But that's just unpleasant.

1 Comment

Thanks, yea I just noticed the import was wrong... sigh

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.