Python: How to prevent python dictionary from putting quotes around my json?
Question: I am using requests to create a post request on a contractor's API. I have a
JSON variable `inputJSON` that undergoes formatting like so:

    
    
    def dolayoutCalc(inputJSON):
        inputJSON = ast.literal_eval(inputJSON)
        inputJSON = json.dumps(inputJSON)
        url='http://xxyy.com/API'
        payload = {'Project': inputJSON, 'x':y, 'z':f}
        headers = {'content-type': 'application/json', 'Accept': 'text/plain'}
        r = requests.post(url, data=json.dumps(payload), headers=headers)
    

My issue arises when I define `payload={'Project':inputJSON, 'x':y, 'z':f}`
What ends up happening is Python places a pair of quotes around the inputJSON
structure. The API I am hitting is not able to handle this. It needs Project
value to be the exact same inputJSON value just without the quotes.

What can I do to prevent python from placing quotes around my `inputJSON`
object? Or is there a way to use requests library to handle such POST request
situation?


Answer: inputJSON gets quotes around it because it's a string. When you call
json.dumps() on something a string will come out, and then when it's converted
to JSON it will get quotes around it. e.g.:

    
    
    >>> import json
    >>> json.dumps('this is a string')
    >>> '"this is a string"'
    

I'm with AKS in that should be able to remove this line:

    
    
    inputJSON = json.dumps(inputJSON)
    

From your description inputJSON sounds like a Python literal (e.g. {'blah':
True} instead of {"blah": true}. So you've used the ast module to convert it
into a Python value, and then in the final json.dumps() it should be converted
to JSON along with everything else.

Example:

    
    
    >>> import ast
    >>> import json
    >>> input = "{'a_var': True}"   # A string that looks like a Python literal
    >>> input = ast.literal_eval(input)   # Convert to a Python dict
    >>> print input
    >>> {'a_var': True}
    >>> payload = {'Project': input}    # Add to payload as a dict
    >>> print json.dumps(payload)
    >>> {"Project": {"a_var": true}}    # In the payload as JSON without quotes
    

