1

I want to push data from a json to firebase without generating a new key for each object. Ideally I would like to define the name of the key.

Here is my code :

import pyrebase
import json

config = {my firebase config}
firebase = pyrebase.initialize_app(config)
db = firebase.database()

with open('myjson.json') as response:
    data = json.load(response)

db.child("customer").push(data)

my json looks like this :

{
  "first_customer": {
    "createdat": "2019-08-09T06:40:10+0000",
    "expires": 1572039000.0,
  },
  "second_customer": {
    "createdat": "2019-08-09T06:33:39+0000",
    "expires": 1570048200.0,
  }
}

but actually my code give me this on firebase :

"customer": {
 "-Lm_M6OM7MCV3_PFvcSH(random_id_given_by_firebase)": {
  "first_customer": {
    "createdat": "2019-08-09T06:40:10+0000",
    "expires": 1572039000.0,
  },
  "second_customer": {
    "createdat": "2019-08-09T06:33:39+0000",
    "expires": 1570048200.0,
  }
 }
}

what i expect is :

"customer": {
  "first_customer": {
    "createdat": "2019-08-09T06:40:10+0000",
    "expires": 1572039000.0,
  },
  "second_customer": {
    "createdat": "2019-08-09T06:33:39+0000",
    "expires": 1570048200.0,
  }
}

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

To control the full path, use set. So something like:

db.child("customer").child("keyforcustomer").set(data)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • i just did db.child("customer").set(data) and it works ! Thanks for your help Frank –  Aug 18 '19 at 18:03