Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json object contains new line character converted to "\n" while converting json object to string or byte

Tags:

java

json

Json object containing new line character gets converted to "\n" while converting json object to string or byte. If I choose any one of the below way it is converting new line character to \n.

byte[] json = objectMapper.writeValueAsBytes(jsonObject);
 String json = objectMapper.writeValueAsString(jsonObject);
 objectMapper.writeValue(json, jsonElection);

Example:

Brown 
And
Ken

Above data got changed as

Brown\nAnd\nKen

Help me to solve this issue.

like image 257
Nagendra V Avatar asked Apr 21 '15 12:04

Nagendra V


People also ask

How do I convert a JSON object to a string?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

Can JSON have new line character?

JSON strings do not allow real newlines in its data; it can only have escaped newlines.


1 Answers

That is the intended working. Newline characters are escaped to the "\n" character sequence.

Your output is perfectly fine and it means the 3 words "Brown", "And" and "Ken" separated by newline characters. When you decode it you will have the exact same text (as your input).

You asked for json text and that is what you got. If a json value contains a newline character, it is represented by the character sequence "\n".

like image 83
icza Avatar answered Oct 09 '22 22:10

icza