Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JSON object from requests.get() to dict

I'm pulling a json object from a url:

import requests
r = requests.get('http://my.endpoint.com/Hardware.json')

Now I need to convert this from r.json() to a dict, so that I can insert it as a document to MongoDB (I think. I'm new to Mongo, soo).

When I try:

import json
d = json.loads(r.json())

It fails with:

TypeError: expected string or buffer

Ultimately, I just want an object type that I can pass into MongoClient().test.hosts.insert_one() as a new document, from pymongo.

like image 966
MrDuk Avatar asked Nov 07 '25 17:11

MrDuk


1 Answers

r.json() can return a dict or a list so there's no need to convert it to JSON.

If it is a dict then insert_one() should work just fine but if it is a list then you need to use insert_many() method instead.

like image 194
Juanín Avatar answered Nov 10 '25 07:11

Juanín



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!