Roshan Bagdiya
Roshan Bagdiya

Reputation: 2178

Convert list of dictionaries to separate list based on dictionary values

I have my JSON Structure like this, I wish to create new list by checking driverKey of each dictionary inside list

{
  "RDBMS": [
    {
      "userName": "a",
      "entityName": "entity1",
      "connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
      "databaseName": "database1",
      "driverKey": "PostgreSQL Unicode"
    },
    {
      "userName": "b",
      "entityName": "entity2",
      "connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
      "databaseName": "database1",
      "driverKey": "PostgreSQL Unicode"
    },
    {
      "userName": "a",
      "entityName": "entity3",
      "connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
      "databaseName": "database1",
      "driverKey": "SQL Server"
    }
  ]
}

Expected Output:

{
  "PostgreSQL": [
    {
      "userName": "a",
      "entityName": "entity1",
      "connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
      "databaseName": "database1",
      "driverKey": "PostgreSQL Unicode"
    },
    {
      "userName": "b",
      "entityName": "entity2",
      "connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
      "databaseName": "database1",
      "driverKey": "PostgreSQL Unicode"
    }
 ],
 "SQL SERVER": [
    {
      "userName": "a",
      "entityName": "entity1",
      "connectionString": "DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;",
      "databaseName": "database1",
      "driverKey": "PostgreSQL Unicode"
    }]

}

I have tried different approach but "connectionString" inside my dictionary gives me different output. loop over list and getting item does not solve my problem. Any Suggestions

Upvotes: 0

Views: 98

Answers (2)

BugHunter
BugHunter

Reputation: 1204

to create a dict, you have iterate items and then assign to the unique key.

v = your_dict

output = {}
for l in v["RDBMS"]:
# check if key exists. if not create a list, to store multiple items
    if l["driverKey"] not in output:
        output[l["driverKey"]] = []
    output[l["driverKey"]].append(l)

Upvotes: 1

cs95
cs95

Reputation: 402263

You'll need two loops. One per key, and another one for each list value per key. Append to a new dictionary per iteration.

data = {'RDBMS' : [...]}

new_data = {}
for k in data:
    for l in data[k]:
        new_data.setdefault(l["driverKey"].split()[0], []).append(l)

Alternatively, use a defaultdict:

from collections import defaultdict

new_data = defaultdict(list)
for k in data:
    for l in data[k]:
        new_data[l["driverKey"].split()[0]].append(l)

The defaultdict is slightly more efficient for a lot of data (the dict.setdefault unnecessarily creates and returns lists regardless of whether or not it has to at each turn).

Based on your comments, the outer loop isn't necessary, but it's always good to write code that won't easily break when your input changes.

print(new_data)
{
    'PostgreSQL': [{
        'userName': 'a',
        'entityName': 'entity1',
        'connectionString': 'DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;',
        'databaseName': 'database1',
        'driverKey': 'PostgreSQL Unicode'
    }, {
        'userName': 'b',
        'entityName': 'entity2',
        'connectionString': 'DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;',
        'databaseName': 'database1',
        'driverKey': 'PostgreSQL Unicode'
    }],
    'SQL': [{
        'userName': 'a',
        'entityName': 'entity3',
        'connectionString': 'DRIVER={PostgreSQL Unicode};DATABASE=database1;UID=uid1;PWD=password;SERVER=127.0.0.1;SID=null;PORT=5432;',
        'databaseName': 'database1',
        'driverKey': 'SQL Server'
    }]
}

Upvotes: 3

Related Questions