1

I am trying to drop elements from JSON. Here is my code:

String test = '[{
                 "type":"new",
                 "color":"red", 
                 "items": ["aa","bb", "cc"]
                }]';
var myJson = jsonDecode(test);
var result = myJson.where((a)=> a != 'items');
print(result);

It does not work. I need to drop items and get: [{"type":"new","color":"red"}]

Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145

1 Answers1

0

In the JSON, test array contains one object item with a property items. Thus, filtering is not caching it. To remove items, you need to map over items and remove the items key from each.

UPDATED:

var result = myJson.map((a)=> {a.remove('items'); return a;} );
Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26