Ask coding questions

← Back to all posts
How do I get nested data in a database?
triplemilkshake (0)

I'm using file from json

  "680546360717606941": {
    "business": "user",
    "workers": {
      "bodyguard": 1

and im trying to do that, but in replit database. how can i get nested data like "business"

Here's an example -

from replit import db

db["greeting"] = {}

db["greeting"]["nested"] = "hi"


What's supposed to happen is, the value for "greeting" will be another key - "nested", and the value for "nested" will be "hi". That is what you would do in a json, but I don't know how to do that in a replit @Spotandjake

Coder100 (17038)

you can't assign into dicts directly, the client has a bug.

Instead, you have to do it like this:

from replit import db

db["greeting"] = {}
temp = db["greeting"]
temp["nested"] = "hi"
db["greeting"] = temp


really dumb, but it is what it is.

triplemilkshake (0)

Apparently replit questions do not indent

Spotandjake (28)

@triplemilkshake firstly use a code lock by using three ` on either side of your code secondly it depend on the language but often what replit does is return the first object so in py like this db[key] and in js db.get().key or whatever the get call is for replit db I can't remember at the moment as I dont use repldb that much pls mark this as the awnser if it worked hope that helps also try to give more detail in future posts pls.