zoplait
zoplait

Reputation: 15

simplejson dumps and multi lines

I have a little question.

I use simplejson to dumps a string. This string contains some new line characters ( \n ), so when I print it on the server side, I get something like that :

toto
tata
titi

And I want that it displays the same way on the client side (html). So I did simply :

return json.dumps(data.replace('\n','<br />'))

And it works, but I don't think it's the good way to do it. Is here another method ?

Thanks.

Upvotes: 0

Views: 898

Answers (1)

Nick Bastin
Nick Bastin

Reputation: 31339

I don't know the specifics of your situation, so maybe this is fine, but in general I'd recommend that you replace \n in the client, not on the server side. If someone wants to use your JSON API for non-HTML client, having <br> will be pretty annoying, and they'll just have to parse that back out. The server should convey the actual data, and the client should be responsible for turning that into information relevant to their user, including changing the formatting or markup if necessary.

Upvotes: 2

Related Questions