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

Tom Sebastian
  • 3,373
  • 5
  • 29
  • 54
  • 1
    Output is correct - your expectation may not be correct. If you want JSON repsentation for content, then, you need to writer custom serializer – Wand Maker Jul 18 '15 at 11:15
  • thanks , here my problem is not related to representation.@JsonRawValue worked for me – Tom Sebastian Jul 18 '15 at 14:24

3 Answers3

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.

Community
  • 1
  • 1
beosign
  • 433
  • 5
  • 10
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.

Community
  • 1
  • 1
Piotr Wittchen
  • 3,853
  • 4
  • 26
  • 39
0

Try to use @JsonRawValue annotation.

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

Community
  • 1
  • 1
Konstantin Pavlov
  • 956
  • 1
  • 10
  • 24