0

I am using cassandra as DB and storing user address as JSON in "address" field of User object.

class User {
  private long id;
  private String name;
  private String address;
}

Then the User row in DB columns as follows

id   |  name  |   address
================================
12345|  jhon  | {"hno":"12-10-46/3","street":"lavella road","city":"begaluru"}

But when I query User object from DB and returned as result of a REST api I see Jackson converting User object to JSON. But I observed that "address" field as string type instead of another JSON object. But I want that "address" should be interpreted as JSON object on client side.

How I could say jackson to convert internal string property to JSON as well?

2 Answers 2

2

I think you should use the annotation @JsonRawValue in your address field.

@JsonRawValue: per-property marker that can be used to specify that the value of property is to be included in serialization ''exactly'' as is, with no escaping or decoration -- useful for embedding pre-serialized JSON (or whatever data format is being used) in output

https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

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

1 Comment

This also works for JSON included JSOn stored as String?
0

It's being treated as a string because it is a string. You'll need to convert it to an object. Not being fluent in Java I managed to find this snippet that uses the javascript engine to eval the string into an object, the same as a browser would do:

 ScriptEngineManager manager = new ScriptEngineManager();
 ScriptEngine engine = manager.getEngineByName("js");        
 Object result = engine.eval([Your Address String Variable Here]);

And then result should be an object derived from the string.

Hope this helps.

Comments

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.