Python - `\n` in json -
i'm using postman send post data. after posting data, body receive in request.body
{ "title": "kill bill: vol. 2","content": "http://netflixroulette.net/api/posters/60032563.jpg\n\nabcdefrefbqwejf\n\nq efjqwefqwrf aksks" }
i want json.loads
body. however, i've new line character in json.
so, first replace \n
\\n
, json.loads
. after replacing \n
\\n
, string receive :-
{\n "title": "kill bill: vol. 2","content":\n "http://netflixroulette.net/api/posters/60032563.jpg\n\nabcdefrefbqwejf\n\nq efjqwefqwrf aksks"\n}
and when json.loads
, results in error. because of new line character @ start of string , @ various other locations well.
any idea how can treat this?
it seems replacing actual newline character '\n' instead of text "\n".
if want remove newline strings "\n" in content, , replace them actual newlines, try replacing "\\n" '\n' (i.e. , not vice versa).
follow-on answer
here's how works, in understanding:
looking @ first , second line of request.body
{ "title": "kill bill ....
there newline character ('\n'
) @ end of first line. don't see it, because being interpreted speak. 1 of many non-printable characters affect formatting when seen , interpreted various programs (e.g. text editor/viewer). trying replace "\n"
's seeing content
part of .json body. these ones:
... 3.jpg\n\nab\n\nq ...
the trouble backslash '\'
used escape normal characters , access special, non-printable characters. asking replace '\n'
'\\n'
replacing line breaks string "\\n"
. can see wanted changed, "\n\n"
's still there, , instead new "\n"
's started appearing (i.e. hence quesiton).
it helps me remember in these cases, how go printing newline string not interpreted newline, shows "\n"
, not like
this (the invisible 1 break). print out string, need let interpreter/text editor understand in fact want '\'
character printed , not follow-on escaped character. because '\'
indicates follow-on escape command, has own escape symbol, itself. is, show '\'
in formatted string, put twice, "\\"
. thus, though looks
... 3.jpg\n\nab\n\nq ...
you have needed format
... 3.jpg\\n\\nab\\n\\nq ...
to way does.
Comments
Post a Comment