-2

So I was trying to use OrderedDict inside json.dumps() and it started off working well. However, when trying to use the output directly inside a payload of an HTTP PUT request, it has these single quotes around it that I believe is screwing with the way the json is being interpreted at the receiving end.

So how do I get around this and have it give me the output without the single quotes?

Example:

out = json.dumps(OrderedDict([("name", 1), ("value", 2)]))

... gives an output such as:

'{"name": 1, "value": 2}'

... when I want it to give me the meat, the json, like:

{"name": 1, "value": 2}

... so that I can put that straight into my

r = requests.post(url, data = out)

... and be on my merry way.

As an aside: is there something VERY basic about strings and string literals (whatever those are) that I am completely missing? My Python knowledge being self taught I am sure there are some gaps.

EDIT: print(out)

... gives

{"name": 1, "value": 2}

which is what I believe I want.

EDIT2: json = out as mentioned in the selected answer did the trick thank you! However, since I am just starting out with coding in Python, I would love to know whether you have come across any articles/ documentation that might be handy for me to know so as to avoid similar issues in the future. Thanks once again everyone!

  • Yeah! I mean the response is bad and everything else has been checked. So it has got to be this. – Sohum Bilawal Nikhil Joshi Nov 30 '18 at 11:49
  • 3
    The obvious thing you are missing is that [json.dump()](https://docs.python.org/3/library/json.html#json.dump) returns a string. The `'{"name": 1, "value": 2}'` is how strings are displayed when inspected - the `'` are just delimiters that are NOT part of the string - the error mus be elsewhere. – Patrick Artner Nov 30 '18 at 11:49
  • Tried that and it gave me `'{"value": 2, "name": 1}'` Last time I tried using extra brackets, it gave me `'{"name": 1, "value": 2}'` so essentially all it gives me is an unordered json dict. – Sohum Bilawal Nikhil Joshi Nov 30 '18 at 11:53
  • Also, if json.dumps() outputs a string, then isn't that not entirely useful? Or is the receiving end json able to read it as json since it has the curly brackets and colons and such? – Sohum Bilawal Nikhil Joshi Nov 30 '18 at 11:54
  • 1
    ... that is the whole point of JSON - you transfer data as string in a kinda stringent format so it can be parsed on the other end ... – Patrick Artner Nov 30 '18 at 11:55
  • Tried the way you said it and this is the error: `invalid character '\'' looking for beginning of value` – Sohum Bilawal Nikhil Joshi Nov 30 '18 at 11:57
  • 2
    Possible duplicate of [Post JSON using Python Requests](https://stackoverflow.com/questions/9733638/post-json-using-python-requests) – Carlos Mermingas Nov 30 '18 at 12:00
  • 2
    Worth noting: the JSON interchange format does not provide any ordering guarantees on key/value pairs. If the order of keys is important, you will need to serialize into JSON using a list of objects rather than just a single object. – Daniel Pryden Nov 30 '18 at 12:01
  • I wonder if part of your problem might be because the POST body is a bytestream, but you're passing a `str` object (a Unicode string) rather than a `bytes` object (a bytestring). Since you're also not passing any `Content-Type` header, I'm not sure how `requests` will serialize the request for you. – Daniel Pryden Nov 30 '18 at 12:07

1 Answers1

1

requests will encode the data for you. You should be able to pass the OrderedDict directly to post:

out = OrderedDict([("name", 1), ("value", 2)])
r = requests.post(url, json=out)

I hope this helps.


EDIT: I realized there's another answer that may help you and it suggests using json instead of data when making the post call.

Documentation:

http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests

Carlos Mermingas
  • 3,822
  • 2
  • 21
  • 40
  • Thank you! I am not 100% sure how (I will make sure to read up about this) but `json = out` did the trick! Maybe it was about being explicit which is something I know Python likes. – Sohum Bilawal Nikhil Joshi Nov 30 '18 at 12:54