Petah
Petah

Reputation: 46040

Create an ObjectNode from JSON string

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.

Upvotes: 17

Views: 33545

Answers (2)

skaffman
skaffman

Reputation: 403501

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.

Upvotes: 11

Petah
Petah

Reputation: 46040

You are using the wrong import.

It should be

com.fasterxml.jackson.databind.node.ObjectNode

Not:

jdk.nashorn.internal.ir.ObjectNode

Upvotes: 11

Related Questions