I have this nicely ordered yet large Json object Array that follows this structure:
{
"object1":[
{
"data1":19.77,
"data2":-0.953125,
"data3":-0.265625,
"id":17231,
"date":"2021-05-14/08:38:47.471"
},
{
"data1":19.77,
"data2":-0.953125,
"data3":-0.265625,
"id":17231,
"date":"2021-05-14/08:38:47.596"
},
{
"data1":19.77,
"data2":-0.9609375,
"data3":-0.2734375,
"id":17231,
"date":"2021-05-14/08:38:47.721"
}
],
"object2":[
{
"data1":19.91,
"data2":-0.9765625,
"data3":-0.109375,
"id":18996,
"date":"2021-05-14/08:38:47.681"
},
{
"data1":19.91,
"data2":-0.9765625,
"data3":-0.109375,
"id":18996,
"date":"2021-05-14/08:38:47.806"
},
{
"data1":19.91,
"data2":-1,
"data3":-0.1171875,
"id":18996,
"date":"2021-05-14/08:38:47.931"
},
{
"data1":19.91,
"data2":-0.96875,
"data3":-0.1015625,
"id":18997,
"date":"2021-05-14/08:38:48.051"
},
{
"data1":19.91,
"data2":-0.96875,
"data3":-0.1015625,
"id":18997,
"date":"2021-05-14/08:38:48.059"
},
{
"data1":19.91,
"data2":-0.9765625,
"data3":-0.109375,
"id":18997,
"date":"2021-05-14/08:38:48.176"
}
]
}
And I've been trying to upload it to firebase with no success, trying to follow this schema in Firestore:
Objects
├───Object1
│ ├───[Upload Timestamp]
│ │ └───Data Array 1
│ ├───[Upload Timestamp]
│ │ └───Data Array 2
│ └───[Upload Timestamp]
│ └───Data Array 3
└───Object2
├───[Upload Timestamp]
│ └───Data Array 1
├───[Upload Timestamp]
│ └───Data Array 2
├───[Upload Timestamp]
│ └───Data Array 3
├───[Upload Timestamp]
│ └───Data Array 4
├───[Upload Timestamp]
│ └───Data Array 5
└───[Upload Timestamp]
└───Data Array 6
I tried to do this with no luck using nested For Loops, Dictionaries and whatnot. So far, I've been only capable of uploading the last Data Array of each object to Firestore.
Is there an way I can properly upload this to Firestore ?
EDIT: Adding more info about my code
What I've got:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import json
from itertools import groupby
import datetime
from datetime import date
today = date.today()
d1 = today.strftime("%d.%m.%Y")
json_file_path = "./myfile.json"
with open(json_file_path, 'r') as j:
contents = json.loads(j.read())
data = {}
#Sorting my json file
for key, items in groupby(sorted(contents, key = lambda x: (x['id'], x['date'])), key=lambda x: x['id']):
data[key] = list(items)
cred = credentials.Certificate("myAppCreds.json")
firebase_admin.initialize_app(cred, {
'projectId': "MyProjectID",
})
db = firestore.client()
for keys in data:
doc_ref = db.collection('Objects').document(keys).collection(d1).document(str(datetime.datetime.now()))
for info in data[keys]:
doc_ref.set(keys)
What happens:
The following entry is created in the Firestore:
Objects/object1/19.05.2021/2021-05-19 13:26:03.933868
Where the document "2021-05-19 13:26:03.933868" is constantly updated with new values, instead of a new one being created for each new value.
The same happens with object2
[Upload Timestamp].objectnode.