Tom Sebastian
Tom Sebastian

Reputation: 3433

Converting java object having string json filed in to JSON

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

Upvotes: 1

Views: 510

Answers (3)

beosign
beosign

Reputation: 433

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.

Upvotes: 1

Konstantin Pavlov
Konstantin Pavlov

Reputation: 985

Try to use @JsonRawValue annotation.

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

Upvotes: 0

Piotr Wittchen
Piotr Wittchen

Reputation: 3922

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.

Upvotes: 0

Related Questions