basickarl
basickarl

Reputation: 40494

Faster XML Jackson: Remove double quotes

I have the following json:

{"test":"example"}

I use the following code from Faster XML Jackson.

JsonParser jp = factory.createParser("{\"test\":\"example\"}");
json = mapper.readTree(jp);
System.out.println(json.get("test").toString());

It outputs:

"example"

Is there a setting in Jackson to remove the double quotes?

Upvotes: 59

Views: 40005

Answers (3)

Thangaraj
Thangaraj

Reputation: 73

jsonValue.get("value").isString().stringValue()

Also check null before calling in single line methods

Upvotes: 0

Jon Polaski
Jon Polaski

Reputation: 144

Simple generic ternary to use the non-quoted text, otherwise keep the node intact.

node.isTextual() ? node.asText() : node

Upvotes: 7

fge
fge

Reputation: 121750

Well, what you obtain when you .get("test") is a JsonNode and it happens to be a TextNode; when you .toString() it, it will return the string representation of that TextNode, which is why you obtain that result.

What you want is to:

.get("test").textValue();

which will return the actual content of the JSON String itself (with everything unescaped and so on).

Note that this will return null if the JsonNode is not a TextNode.

Upvotes: 116

Related Questions