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.
- Why is it single quoting my double quoted items?
- Can I force the use of double-quotes so that it respects the escape characters provided by json.dumps()
- 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...