yodellingbutters
yodellingbutters

Reputation: 345

How to add Dictionary in a Dictionary in Python 3+?

My goal is to get the response back as something like following:

{
 "modules": [
    {
        "id": 1,
        "modulename": "Dashboard",
        "order": 1,
        "defaultModule": true,
        "isSelected": false
    },{},{}....
]
"menuItems": [
    {
        "id": 30,
        "order": 0,
        "module": 5,
        "roleName": null,
        "name": "Controls Overivew",
        "pagetitle": "Contorls View",
        "enabled": true,
        "isexternal": false,
        "url": "",
        "apppath": "/criticalcontrols/criticalcontrols",
        "icon": "nav-icon icon-energy",
        "isDefault": true
    },{},{}...
]
}

Currently with the following I am getting only one value out of content

 cursor.execute("SQL_QUERY_HERE")
    rv1 = cursor.fetchall()
    #payload = [] # List
    content = {} # Dict
    data = {}
    for r in rv1:
        content = { 
                    "id": r["id"], 
                    "name": r["name"], 
                    "pagetitle": r["pagetitle"], 
                    "order": r["order"], 
                    "module": r["module"], 
                    "enabled": "true" if r["enabled"] ==  "b'\x01'" else "false", 
                    "isexternal": "true" if r["isexternal"] == "b'\x01'" else "false", 
                    "url": r["url"], 
                    "apppath": r["apppath"], 
                    "icon": r["icon"], 
                    "isDefault": "true" if r["isDefault"] == "b'\x01'" else "false"
        }
        #payload.append(content)
        data["modules"] = [content]
        content = {}



    cursor.execute("SQL_QUERY_HERE")
    rv2 = cursor.fetchall()
    for r in rv2:
        content = { 
                    "id": r["id"],
                    "modulename": r["modulename"],
                    "order": r["order"],
                    "defaultModule": "true" if r["defaultModule"] == "b'\x01'" else "false",
                    "isSelected": "true" if r["enabled"] == "b'\x01'" else "false"
        }
        #payload.append(content)
        data["menuItems"] = [content]
        content = {}

    resp = jsonify(data)

which is something like only one entry for modules and one for menuItems

Upvotes: 0

Views: 30

Answers (1)

Your problem is that you are overwriting data["modules"] with every module you analyze. Try the following:

cursor.execute("SQL_QUERY_HERE")
rv1 = cursor.fetchall()
#payload = [] # List
content = {} # Dict
data = {}
for r in rv1:
    content = { 
                "id": r["id"], 
                "name": r["name"], 
                "pagetitle": r["pagetitle"], 
                "order": r["order"], 
                "module": r["module"], 
                "enabled": "true" if r["enabled"] ==  "b'\x01'" else "false", 
                "isexternal": "true" if r["isexternal"] == "b'\x01'" else "false", 
                "url": r["url"], 
                "apppath": r["apppath"], 
                "icon": r["icon"], 
                "isDefault": "true" if r["isDefault"] == "b'\x01'" else "false"
    }
    #payload.append(content)
    data["modules"] = data.get("modules", [])  # if modules is not defined, get an empty list
    data["modules"].append(content)
    content = {}

cursor.execute("SQL_QUERY_HERE")
rv2 = cursor.fetchall()
for r in rv2:
    content = { 
                "id": r["id"],
                "modulename": r["modulename"],
                "order": r["order"],
                "defaultModule": "true" if r["defaultModule"] == "b'\x01'" else "false",
                "isSelected": "true" if r["enabled"] == "b'\x01'" else "false"
    }
    #payload.append(content)
    data["menuItems"] = data.get("menuItems", [])  # if menuItems is not defined, get an empty list
    data["menuItems"].append(content)
    content = {}

resp = jsonify(data)

It is important to keep in mind the nested structures: data is a dictionary that contains lists of dictionaries, so data is a dict, data["modules"] is a list (and therefore you add items using append) and data["modules"][0] is again a dictionary where you can find the definitions you need.

Upvotes: 1

Related Questions