How can I add \n(newline) In Json string

OK, this is an error message of the server. It has nothing to do with Python. You need to learn about what exactly the API expects in your request.

The text I have shown to you was just an example. I have no idea what your server accepts as requests.

In my example the trailing newline is just a whitespace which does not change the data inside JSON. If you need to have a newline inside a string inside JSON you can use the two literal characters \n which means you will need to double the backslash to tell Python to produce a single regular backslash (instead of treating it as a special character).

Let’s take my example and add \n to the property value of firstName:

data = '{"firstName": "John\\n"}\n'
#                               ^^--- These two characters create a newline
#                                     character in the resulting string.
#                          ^^--- The two backslashes create a single literal
#                                literal backslash \ in the resulting string.
#                                The characters \n are then interpreted in JSON
#                                as a newline character.
2 Likes