Skip to content

Instantly share code, notes, and snippets.

@datamafia
Created February 23, 2016 20:19

Revisions

  1. datamafia created this gist Feb 23, 2016.
    12 changes: 12 additions & 0 deletions shopify_helper.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    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