0

I know there are a few similar questions, but I haven't seen anything that quite address the specifics I'm running into, so I'm hoping for some enlightenment.

Working on a pretty common problem taking output from one API and trying to shove it into another. The output from the originating API can include some HTML, which normally isn't a problem. json.dumps() takes care of most everything here without issues, which from what I've read is the most recommended method of dealing with escaping user input.

However, anything with double-quotations is getting screwed up. I'll post some code.

updateProblemReport = []
# Get the new description
description = getWrikeFieldData("description", taskID)
# result: <p style="padding-left: 1.5em;">A paragraph.</p>
description = json.dumps(description)
# result: "<p style=\"padding-left: 1.5em;\">A paragraph.</p>"
updateProblemReport.append({"attributes": {
                "DETAILS": description, "OBJECTID": problemReport.get_value('OBJECTID')}})
# result: {'attributes': {'DETAILS': '"<p style=\\"padding-left: 1.5em;\\">A paragraph.</p>"', 'OBJECTID': 139}}

This results in my DETAILS value being mangled and subsequent rejection from the DB it's supposed to update. I guess this leads to a couple of questions.

  1. Why is it single quoting my double quoted items?
  2. Can I force the use of double-quotes so that it respects the escape characters provided by json.dumps()
  3. Why does it show a double forward slash as a final result?

A note on something I noticed while typing this up that has me even more confused. If I remove the following line.

description = json.dumps(description)

to just ignore escape sequences the end result json appended to updateProblemReport looks like this:

{'attributes': {'DETAILS': '<p style="padding-left: 1.5em;">A paragraph.</p>', 'OBJECTID': 139}}

This leaves me completely confused on why I'm getting a double \\ as the final result in the original...

martineau
  • 119,623
  • 25
  • 170
  • 301
Roger Asbury
  • 351
  • 1
  • 4
  • 13
  • What is the point of the `json.dumps`? I've never seen any recommendation to use `json.dumps` to escape user input (because it doesn't work as you want it to). – SuperStormer Jul 30 '22 at 01:01
  • json.dumps adds escape characters to quotes as shown in the example above. In my searches for an answer to "How best to escape characters for insertion into a json string" it came up in most of the answers as the recommended answer. I've tried "re.escape(string)" but it escapes EVERYTHING including white space. – Roger Asbury Aug 01 '22 at 16:59
  • "Why is it single quoting my double quoted items?" Because that **isn't JSON**; it's structured data within the Python program. Notice how the `description` part of the input still uses double quotes, because it is **a string** that **represents JSON data**. The overall question cannot be answered because it is not coherent; it seems that you have a *general* misunderstanding about *data types* and *representations* that can't be cleared up in a single question. – Karl Knechtel Aug 04 '22 at 22:32
  • "Why does it show a double forward slash as a final result?" I can't understand what this is trying to say. I don't see a double forward slash anywhere in the post. If you mean the double **backslash**, see https://stackoverflow.com/questions/24085680. – Karl Knechtel Aug 04 '22 at 22:34

0 Answers0