-1

i want to remove the json data from the list based on the allocation.If allocation:True then, that particular josn should remove from the dictionary. in the below data {"paperWidth": "4","allocation": True,"model": "RB3150","connType": ["Lan","Wifi","BlueTooth"],"commandSet": "TVSPOS"}

so this should remove from my final object.How to do this in python.

d = {"allPrinters": [{"models": [{"paperWidth": "4","allocation": True,"model": "RB3150","connType": ["Lan","Wifi","BlueTooth"],"commandSet": "TVSPOS"},{"paperWidth": "4","allocation": False,"model": "RB3151","connType": ["Lan","Wifi"],"commandSet": "TVSPOS"}],"active": True,"make": "TVS","_id": "59e6f670f532eb1260b50cd9"},{"models": [{"paperWidth": "4","allocation": False,"model": "EP3160","connType": ["Lan","Wifi"],"commandSet": "ESCPOS"},{"paperWidth": "4","allocation": True,"model": "EP3161","connType": ["Lan"],"commandSet": "ESCPOS"}],"active": True,"make": "EPSON","_id": "59e6fc8ff532eb1260b50cf4"}]}

Thank You, Ramesh M

user1335606
  • 479
  • 2
  • 5
  • 14
  • possible duplicate of : https://stackoverflow.com/questions/19167485/removing-json-property-in-array-of-objects-with-python and : https://stackoverflow.com/questions/36606930/delete-an-element-in-a-json-object and :https://stackoverflow.com/questions/19201233/how-to-delete-json-object-using-python – DRPK Nov 01 '17 at 14:42
  • Possible duplicate of [Removing JSON property in array of objects with Python](https://stackoverflow.com/questions/19167485/removing-json-property-in-array-of-objects-with-python) – DRPK Nov 01 '17 at 14:42

1 Answers1

1

I'm not sure if the thing you want to remove is the whole printer or just the model, but this should work for the former case and is simple to convert to the latter (dump the continue and replace d['allPrinters'] with model):

for i, printer in enumerate(d['allPrinters']):
     for model in printer['models']:
         if model['allocation']:
             d['allPrinters'].pop(i)
             continue
Dominik Stańczak
  • 2,046
  • 15
  • 27