-2

How can I escape a string for usage within a json document?

Something like:

notEscaped = '"/\""\\'
escaped = jsonEscape(notEscaped)
newNotEscaped = jsonUnEscape(escaped)

Here are some of the references I've looked at:

Steve
  • 911
  • 1
  • 6
  • 19
Wayne Workman
  • 415
  • 5
  • 13
  • Not a strict answer to your question but if you use a library like python's json, it'll take care of the escaping for you – Steve Feb 21 '18 at 22:22
  • 3
    Any **valid** string can be put in a *json*. What's the real question? – CristiFati Feb 21 '18 at 22:22
  • It's [right there in the standard library](https://docs.python.org/2/library/json.html), included with the Python interpreter out-of-the-box. – Charles Duffy Feb 21 '18 at 22:26
  • That said, insofar as you *are* asking for people to suggest a library to you -- note https://stackoverflow.com/help/on-topic, particularly the "some questions are still off-topic" list, particularly entry #4 (*"Questions asking us to recommend or find a book, tool, **software library,** tutorial or other off-site resource..."*) – Charles Duffy Feb 21 '18 at 22:27

1 Answers1

5

You can use python's standard json library

import json
json.dumps(myString)
Steve
  • 911
  • 1
  • 6
  • 19
  • 2
    Note [How to Answer](https://stackoverflow.com/help/how-to-answer), particularly the section "Answer Well-Asked Questions", particularly the bullet regarding questions which *"are not about programming as defined in the help center"* -- linking to a page that explicitly says that library recommendation requests are off-topic. – Charles Duffy Feb 21 '18 at 22:27
  • Exactly what I was looking for. – Wayne Workman Feb 21 '18 at 22:36