How to convert the list of JSON frames to a JSON frame

advertisements

I want to convert List of JSON object ot Single JSON frame

Here is my code

for i in user1:
    name=i.name
    password=i.password
    id1=i.id
    user = { "name" : name,
              "password" : password,
              "id":id1}
    user = json.dumps(user)
    userid.append(user)

I am getting output is

['{"password": "A123", "name": "Sam", "id": 1}', '{"password": "B123", "name": "Sammy", "id": 2}', '{"password": "C123", "name": "Abhi", "id": 3}']

What I want is something like

{{"password": "A123", "name": "Sam", "id": 1},{"password": "B123", "name": "Sammy", "id": 2},{"password": "C123", "name": "Abhi", "id": 3}}


json.dumps() returns a string, so you're append strings to userid—and thus end up with the list of strings shown.

If you want a dictionary of dictionaries using the id as the primary key, all you need is:

users = {i.id: {"name": i.name, "password": i.password} for i in user1}