I am trying to create a JSON
object and send it to the Firebase Database
using python
, but when I do this I get:
TypeError: Object of type 'Tag' is not JSON serializable
Here is my code:
data = {'address': address,
'name': name
}
print(type(data))
sent = json.dumps(data)
result = firebase.post("/tHouse/houseTest", sent)
The is something wrong with json.dumps(data)
since the error is pointed out here. Running print(type(data))
returns though <class 'dict'>
.
Also the name
and address
are set beforehand
Being a bs4.element.Tag
, address
can not be serialised to JSON.
How you handle this depends on what part of the tag you want to store in your db. If you just call str()
on the Tag
the output will include the XML/HTML markup. If you want the text contained within the tag, access the .text
attribute e.g.
from bs4 import BeautifulSoup
soup = BeautifulSoup('<address>1 Some Street Somewhere ABC 12345</address>')
address = soup.address
>>> type(address)
<class 'bs4.element.Tag'>
>>> str(address)
'<address>1 Some Street Somewhere ABC 12345</address>'
>>> address.text
u'1 Some Street Somewhere ABC 12345'
So this might be what you need to do:
data = {'address': address.text, 'name': 'Some One'}
>>> json.dumps(data)
'{"name": "Some One", "address": "1 Some Street Somewhere ABC 12345"}'