1

In our application one database table having some result stored as json as below:

---------------------------------------------------- -------------
content                                             | other fields...
---------------------------------------------------- --------------
"{ \"key\":[\"value1\",\"value2\",\"value3\"]}"     | 12 ...

I need to fetch and write into a result file , the content field of a set of records as a single json like: (Expected)

[
  {
    "content": {
      "key": [
        "value1",
        "value2",
        "value3"
      ]
    }
  }
  .....
]

In the orresponding java entity I put @JsonIgnore for all fields except content.

class Result{
//@JsonIgnore
//otherfields
 ....
@Column("content")
private String content;//the json string field
....
}

But when I read from db and write to file using:

ObjectWriter writer = new ObjectMapper().writer().withDefaultPrettyPrinter();
        writer.writeValue(new File(outFile), entityList);

I got file as: (Original)

[ 
    {
      "content" : "{ \"key\":[\"value1\",\"value2\",\"value3\"]}"
    } 
....
]

You may notice the issue. It take the jason field as a string and put as the value for the key "content", instead of a nested jason as expected

2
  • 1
    Output is correct - your expectation may not be correct. If you want JSON repsentation for content, then, you need to writer custom serializer Commented Jul 18, 2015 at 11:15
  • thanks , here my problem is not related to representation.@JsonRawValue worked for me Commented Jul 18, 2015 at 14:24

3 Answers 3

1

According to How can I include raw JSON in an object using Jackson? you can try to annotate the content with @JsonRawValue:

class Result {
    @JsonRawValue
    private String content;
}

This will output:

[ {
  "content" : { "key":["value1","value2","value3"]}
} ]

which is semantically what you want. However, you expected the outout to be pretty formatted. This can be achieved by specifying a serializer, see also Convert JSON String to Pretty Print JSON output using Jackson :

class Result {
    @JsonRawValue
    @JsonSerialize(using = ToPrettyJsonSerializer.class)
    private String content;
}

private static class ToPrettyJsonSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String string, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
        Object json = new ObjectMapper().readValue(string, Object.class);
        gen.writeObject(json);
    }
}

This outputs:

[ {
  "content" : {
    "key" : [ "value1", "value2", "value3" ]
  }
} ]

It is not exactly the format you expected, but getting close. Hope that helps.

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

Comments

0

I think, backlashes before quotation marks cause the problem and whole JSON data is treated as String instead of JSON object. You can try to remove backslashes before transforming JSON into object. One of the solutions I found is here: https://stackoverflow.com/a/19871960/1150795.

Comments

0

Try to use @JsonRawValue annotation.

How can I include raw JSON in an object using Jackson?

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.