1

I have a bit complicated json response from a server, below is my json:

    services = [{"Mobile": [{"name": "CompanyA", id: 0, address: "XYZ"},  
    {"name": CompanyB, id: 1, address: "QWE"},  
    {"name": CompanyC, id: 2, address: "TYU"}]  
    },
    {"Computer": [{"name": "CompanyD", id: 3, address: "PPP"},  
    {"name": CompanyD, id: 4, address: "UYU"},  
    {"name": CompanyE, id: 5, address: "NMB"}]  
    }]   

I need to construct new dictionary which only holds bellow data:

services = [{"Mobile": [{"name": "CompanyA"},{"name": "CompanyB"}, 
          {"name": "CompanyC"}]},
          {"Computer": [{"name": "CompanyD"},{"name": "CompanyD"},  
          {"name": "CompanyE"}]  
    }]  

in other words, delete id and address fields.

NSA NSA
  • 13
  • 2
  • Does this answer your question? [Remove element from dictionary by key while iterating](https://stackoverflow.com/questions/44074433/remove-element-from-dictionary-by-key-while-iterating) – Devang Sanghani Feb 14 '22 at 07:42
  • Almost, but see my values are a list by itself, so it's a nested list. How I could access them? @DevangSanghani – NSA NSA Feb 14 '22 at 07:44

4 Answers4

1

Iterating through each nested dictionary/list and deleting the unwanted keys,

for i in services:
    for j in i.values():
        for k in j:
            del k[id]
            del k["address"]
            
print(services)

Output:
[{'Mobile': [{'name': 'CompanyA'}, {'name': 'CompanyB'}, {'name': 'CompanyC'}]}, {'Computer': [{'name': 'CompanyD'}, {'name': 'CompanyD'}, {'name': 'CompanyE'}]}]
fractal397
  • 534
  • 1
  • 5
  • 11
0

You can iterate over all values in a nested dictionary and delete:

D = {'emp1': {'name': 'Bob', 'job': 'Mgr'},
         'emp2': {'name': 'Kim', 'job': 'Dev'},
         'emp3': {'name': 'Sam', 'job': 'Dev'}}
    for id, info in D.items():
        print("\nEmployee ID:", id)
        for key in info:
            print(key + ':', info[key])
    # Prints Employee ID: emp1
    #        name: Bob
    #        job: Mgr
    #        Employee ID: emp2
    #        name: Kim
    #        job: Dev
    #        Employee ID: emp3
    #        name: Sam
    #        job: Dev

You can see more about here: https://www.learnbyexample.org/python-nested-dictionary/

0

if your data is something like this:

services = [
    {
        "Mobile": 
            [
                {"name": "CompanyA", 'id': 0, 'address': "XYZ"},  
                {"name": 'CompanyB', 'id': 1, 'address': "QWE"},  
                {"name": 'CompanyC', 'id': 2, 'address': "TYU"}
            ]  
    },
    {
        "Computer": 
            [
                {"name": "CompanyD", 'id': 3, 'address': "PPP"},  
                {"name": 'CompanyD', 'id': 4, 'address': "UYU"},  
                {"name": 'CompanyE', 'id': 5, 'address': "NMB"}
            ]  
    }
]    

you can iterate over them and save in a new variable like this:

data = []
for i in services:
    for index, j in i.items():
        data.append({index : [ {'name': k['name']} for k in j]})

and the compact command would be like this:

data = [{index: [{'name': k['name']} for k in j] for index, j in i.items()} for i in services]

the data will be like this:

[
   {
      "Mobile":[
         { "name":"CompanyA" },
         { "name":"CompanyB" },
         { "name":"CompanyC" }
      ]
   },
   {
      "Computer":[
         { "name":"CompanyD" },
         { "name":"CompanyD" },
         { "name":"CompanyE" }
      ]
   }
]
Omid Roshani
  • 1,083
  • 4
  • 15
0

For data:

services = [{"Mobile": [{"name": "CompanyA", 'id': 0, 'address': "XYZ"},  
                        {"name": 'CompanyB', 'id': 1, 'address': "QWE"},  
                        {"name": 'CompanyC', 'id': 2, 'address': "TYU"}]  
            },
            {"Computer": [{"name": "CompanyD", 'id': 3, 'address': "PPP"},  
                          {"name": 'CompanyD', 'id': 4, 'address': "UYU"},  
                          {"name": 'CompanyE', 'id': 5, 'address': "NMB"}]  
            }]

A combination of list and dictionary comprehensions will achieve the result you're looking for.

[{k: [{'name': v2['name']} for v2 in v] for k, v in d.items()} for d in services]

Result:

[{'Mobile':   [{'name': 'CompanyA'}, {'name': 'CompanyB'}, {'name': 'CompanyC'}]}, 
 {'Computer': [{'name': 'CompanyD'}, {'name': 'CompanyD'}, {'name': 'CompanyE'}]}]
Chris
  • 26,361
  • 5
  • 21
  • 42