I want to create an API that has to recover data from firestore and expose them in json but I am facing the error of:
Object of type GeoPoint is not JSON serializable
What's wrong? This My code:
#!/usr/bin/env python
import firebase_admin
import json
from pprint import pprint as pp
from flask import Flask, flash, request, url_for, jsonify, Response
from firebase_admin import credentials
from firebase_admin import firestore
app = Flask(__name__)
cred = credentials.Certificate('xxxx.json')
firebase_admin.initialize_app(cred)
db = firestore.client()
@app.route('/')
def index():
markers_ref = db.collection(u'Test')
docs = markers_ref.get()
data = []
for doc in docs:
data.append(doc.to_dict())
json_data = json.dumps(data)
resp = Response(json_data, status=200, mimetype='application/json')
return resp
if __name__=='__main__':
app.run(debug=True)
Following should do the trick -
from flask import (Flask, request, session, g, redirect, url_for, abort, render_template, flash, Response, jsonify)
import firebase_admin
from firebase_admin import firestore
@app.route('/foods/<id>')
def read_food(id):
result=FOODS.document(id).get().to_dict()
record = {}
# Populate the new dict with JSON serializiable values
for key in result:
if isinstance(result[key], firebase_admin.firestore.GeoPoint):
record[key] =str(result[key].latitude)+','+str(result[key].longitude)
continue
record[key] = result[key]
# Add the key so that we have a reference to the record
record['key'] = result[key]
return jsonify(record)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.