Skip to content

Instantly share code, notes, and snippets.

@datamafia
Created February 23, 2016 20:19
Show Gist options
  • Save datamafia/e45c24eb8e1f73d18edc to your computer and use it in GitHub Desktop.
Save datamafia/e45c24eb8e1f73d18edc to your computer and use it in GitHub Desktop.
Shopify API helper
def prepare_for_api(str_json):
"""
Prepare a json string API use by escaping double quotes and removing \n new line breaks
:param str_json: string
:return: string
Note: Use pattern is to wrap data at the last possible moment to avoid extra escaping.
"""
str_json = re.sub(' +', ' ', str_json) # kills multi whitespace
str_json = str_json.replace('\n', '') # kill new line breaks caused by triple quoted raw strings
str_json = str_json.replace('"', '\\"') # address double quotes
# Should be json ready. If not, add more.
return str_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment