I want to export a Firebase object from JavaScript as JSON and download it. For example, this item that is in the patients/
reference. I would like to download it on a .json file with this format:
"-LCZPCkiGCNhFaQ8ckJ-" : {
"altura" : 165,
"apellido" : "Salas",
"extra" : {
"Jubilado" : "No",
"Localidad" : "Madrid",
"Telefono" : "698532147"
},
"fechaNacimiento" : "14/10/1961",
"nombre" : "Paquita",
"sexo" : "Mujer"
}
I have only been able to download a file stored in Storage but not in Realtime Database
firebase.storage().ref('grabaciones/').child(grabacion).getDownloadURL().then(function (url) {
let a = document.createElement("a");
a.download = grabacion;
a.href = url;
document.body.appendChild(a);
a.click();
}).catch(function (error) {
// Handle any errors
console.log(error);
});
Thank you in advance.
Updated code, where the element is obtained as a JSON and is downloaded as a .json. Only works in Firefox:
$scope.exportarJSON = function (paciente) {
console.log(grabacion);
firebase.database().ref('pacientes/').child(pacinte).once('value', function (paciente) {
download(paciente + ".json", JSON.stringify(paciente.val()));
});
};
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/json;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}