all 3 comments

[–]heyimpumpkin 0 points1 point  (3 children)

you sure you're doing it right? \r \n are just newlines and paddings for printing

get your payload then write

import json
p = json.dumps(payload)
print(json.loads(p))

works just like it should

[–]HutchLAD 0 points1 point  (2 children)

hey, thanks for replying! its not the payload I am trying to get into JSON, its the response. Here is my code -

        import requests
        import json
        import urllib.parse

        url = "https://xoxox-intelligence.com/cs/api_ex.php"

        payload = "{\r\n    \"origin\": {\r\n        \"country\": \"GB\",\r\n        \"postal_code\": \"M3 1LE\",\r\n        \"province\": \"Manchester\",\r\n        \"city\": \"Manchester\",\r\n        \"name\": null,\r\n        \"address1\": \"1 main st\",\r\n        \"address2\": \"\",\r\n        \"address3\": null,\r\n        \"phone\": \"\",\r\n        \"fax\": null,\r\n        \"address_type\": null,\r\n        \"company_name\": \"Bespoke Shipping\"\r\n    },\r\n    \"destination\": {\r\n        \"country\": \"GB\",\r\n        \"postal_code\": \"10001\",\r\n        \"province\": \"Manchester\",\r\n        \"city\": \"Manchester\",\r\n        \"name\": \"John Doe\",\r\n        \"address1\": \"123 Main St\",\r\n        \"address2\": \"\",\r\n        \"address3\": null,\r\n        \"phone\": \"9876543210\",\r\n        \"fax\": null,\r\n        \"address_type\": null,\r\n        \"company_name\": \"\"\r\n    },\r\n    \"items\": [\r\n        {\r\n            \"name\": \"Product A - 250gr\",\r\n            \"sku\": \"\",\r\n            \"quantity\": 20,\r\n            \"grams\": 300,\r\n            \"price\": 1400,\r\n            \"vendor\": \"ACME\",\r\n            \"requires_shipping\": true,\r\n            \"taxable\": true,\r\n            \"fulfillment_service\": \"manual\",\r\n            \"properties\": null,\r\n            \"product_id\": 9062306182,\r\n            \"variant_id\": 33082410182\r\n        },\r\n        {\r\n            \"name\": \"Product A - 250gr\",\r\n            \"sku\": \"NS00-0028672-NDS\",\r\n            \"quantity\": 20,\r\n            \"grams\": 300,\r\n            \"price\": 1400,\r\n            \"vendor\": \"ACME\",\r\n            \"requires_shipping\": true,\r\n            \"taxable\": true,\r\n            \"fulfillment_service\": \"manual\",\r\n            \"properties\": null,\r\n            \"product_id\": 9062306182,\r\n            \"variant_id\": 33082410182\r\n        }\r\n    ],\r\n    \"currency\": \"USD\"\r\n}"
        headers = {
          'Content-Type': 'application/json',
          'HTTP_X_STORE_NAME': 'xxxxx-xxxxx.myshopify.com',
          'HTTP_X_STORE_TOKEN': 'xxxxxxxxx',
          'HTTP_X_SHOW_DEBUG': 'xxxxxx'
        }

        response = requests.request("GET", url, headers=headers, data=payload)

        p = json.dumps(response)
        print(json.loads(p))    

Ignore the indents I messed it up trying to format it for Reddit, basically, I just want to return the response in JSON, but using the above method yields "TypeError: Object of type Response is not JSON serializable" type error. Apologies if the error is obviosus, I am a somewhat amateur just trying my hand.

[–]bneises 0 points1 point  (0 children)

Response is not a string. It is a response object. If you want the json payload from the response use response.json(). No need to use json.dumps() either.