I'm trying to upload a JSON file to a Firebase realtime database but I keep getting error 400. I've uploaded the same JSON file manually through the firebase website and the upload is completed successfully.
I've checked the firebase-debug.log file and I noticed the JSON file is being sent without the filename, but I'm unsure as to why that is.
[debug] [2024-07-11T18:49:42.872Z] >>> [apiv2][query] PUT dbpath.firebaseio.com/users/uid/works/.json
[debug] [2024-07-11T18:49:42.873Z] >>> [apiv2][body] PUT dbpath.firebaseio.com/users/uid/works/.json [stream]
[debug] [2024-07-11T18:49:43.291Z] <<< [apiv2][status] PUT dbpath.firebaseio.com/users/uid/works/.json 400
[debug] [2024-07-11T18:49:43.292Z] <<< [apiv2][body] PUT dbpath.firebaseio.com/users/uid/works/.json {"error":"Invalid data; couldn't parse JSON object, array, or value."}
[debug] HTTP Error: 400, Invalid data; couldn't parse JSON object, array, or value.
[error]
[error] Error: Unexpected error while setting data: FirebaseError: HTTP Error: 400, Invalid data; couldn't parse JSON object, array, or value.
Here's the piece of bash code I'm running (this code is being ran after verifying the files are valid and both the var and uid files have the same amount of user data):
for ((i=0; i<${#uids[@]}; i++)); do
uid=${uids[$i]}
var=${vars[$i]}
varFile=$var
varFile=${var//[-]/_}
# Simular la llamada a ORCID y guardar el resultado en un archivo JSON
echo "Realizando solicitud a ORCID para ${var}"
echo $varFile
jsonFilePath="/Users/user/Documents/folder/outputs_json/prueba/works${varFile}.json"
curl -H 'Content-Type: application/orcid+json' -H 'Authorization: Bearer authkey' "https://pub.orcid.org/v3.0/${var}/works" -i -o "$jsonFilePath"
jsonFile="works${varFile}.json"
# Quitar las primeras 15 líneas del archivo JSON
tail -n +17 "$jsonFile" > "${jsonFile}.tmp" && mv "${jsonFile}.tmp" "$jsonFile"
# Actualizar Firebase con el UID
echo "Actualizando Firebase para el UID ${uid}"
echo $jsonFile
firebase database:set "/users/${uid}/works/" --data ${jsonFile} -P project-name
done
I'm stumped because I've changed the --data value to full filenames and different variables but I'm still getting the same error. I've also verified the filenames and paths are being correctly stored in the variables by printing them. Any help will be really appreciated!
Edit: Here's an example JSON file:
{
"last-modified-date" : {
"value" : 1685720662037
},
"group" : [ {
"last-modified-date" : {
"value" : 1685720662037
},
"external-ids" : {
"external-id" : [ {
"external-id-type" : "doi",
"external-id-value" : "10.1111/tpj.15599",
"external-id-normalized" : {
"value" : "10.1111/tpj.15599",
"transient" : true
},
"external-id-normalized-error" : null,
"external-id-url" : {
"value" : "https://doi.org/10.1111/tpj.15599"
},
"external-id-relationship" : "self"
} ]
},
"work-summary" : [ {
"put-code" : 136214894,
"created-date" : {
"value" : 1685720662037
},
"last-modified-date" : {
"value" : 1685720662037
},
"source" : {
"source-orcid" : null,
"source-client-id" : {
"uri" : "https://orcid.org/client/0000-0001-9884-1913",
"path" : "0000-0001-9884-1913",
"host" : "orcid.org"
},
"source-name" : {
"value" : "Crossref"
},
"assertion-origin-orcid" : null,
"assertion-origin-client-id" : null,
"assertion-origin-name" : null
},
"title" : {
"title" : {
"value" : "The small RNA‐mediated gene silencing machinery is required in Arabidopsis for stimulation of growth, systemic disease resistance, and suppression of the nitrile‐specifier gene NSP4 by Trichoderma atroviride"
},
"subtitle" : null,
"translated-title" : null
},
"external-ids" : {
"external-id" : [ {
"external-id-type" : "doi",
"external-id-value" : "10.1111/tpj.15599",
"external-id-normalized" : {
"value" : "10.1111/tpj.15599",
"transient" : true
},
"external-id-normalized-error" : null,
"external-id-url" : {
"value" : "https://doi.org/10.1111/tpj.15599"
},
"external-id-relationship" : "self"
} ]
},
"url" : {
"value" : "https://doi.org/10.1111/tpj.15599"
},
"type" : "journal-article",
"publication-date" : {
"year" : {
"value" : "2022"
},
"month" : {
"value" : "02"
},
"day" : null
},
"journal-title" : {
"value" : "The Plant Journal"
},
"visibility" : "public",
"path" : "/0000-0001-7872-3135/work/136214894",
"display-index" : "0"
} ]
},
],
"path" : "/0000-0001-7872-3135/works"
}
The command you're using isn't correct:
firebase database:set "/users/${uid}/works/" --data ${jsonFile} -P project-name
The --data
flag introduces an actual JSON object string as the next parameter, not the name of a file to import. You can tell what the CLI expects using firebase database:set --help
:
Usage: firebase database:set [options] <path> [infile]
store JSON data at the specified path via STDIN, arg, or file
Options:
-d, --data <data> specify escaped JSON directly
-f, --force pass this option to bypass confirmation prompt
--instance <instance> use the database <instance>.firebaseio.com (if omitted, use default database
instance)
--disable-triggers suppress any Cloud functions triggered by this operation
-h, --help output usage information
This help is telling you that you should you provide the name of the file as its own argument without a flag. So your command should be more like this:
firebase -P project-name database:set "/users/${uid}/works/" "${jsonFile}"