Home > Software engineering >  DART - Filter JSON Data
DART - Filter JSON Data

Time:05-11

I'm trying to filter the json data. There is a field called "brand" in my json (so basically I'm trying to filter the data by brands)

This is how my json looks

{
  "items": [
    {
      "_id": "30baa1ca-4186-4ff0-abe8-a5970e753444",
      "_owner": "1d3480e5-0eda-47ef-8406-38d89bf15ded",
      "_createdDate": "2022-05-09T08:47:29.137Z",
      "discountedPrice": "44.97",
      "_updatedDate": "2022-05-09T08:48:44.147Z",
      "getDealLink": "https://amzn.to/3FqBq4O",
      "brands": [
        "Amazon"
      ],
      "title": "Mellanni Extra Deep Pocket Twin XL Sheet Set ",
      "amazonlogo": "wix:image://v1/1d3480_ffad681242174f799ddea471e649ef7b~mv2.png/amazon_PNG24.png#originWidth=1024&originHeight=346",
      "save": "#1 Best Seller",
      "link-items-all": "/items/",
      "link-items-title": "/items/mellanni-extra-deep-pocket-twin-xl-sheet-set-"
    },
    {
      "_id": "a7d3aaa8-9654-4535-b6c5-b147ff0d8eb3",
      "_owner": "1d3480e5-0eda-47ef-8406-38d89bf15ded",
      "_createdDate": "2022-05-08T22:35:38.398Z",
      "discountedPrice": "$81.59",
      "_updatedDate": "2022-05-08T22:39:52.801Z",
      "getDealLink": "https://amzn.to/3ymXGLe",
      "brands": [
        "Amazon"
      ],
      "originalPrice": "$199.99",
      "title": "2 Pack Stadium chairs for bleachers with back support",
      "amazonlogo": "wix:image://v1/1d3480_ffad681242174f799ddea471e649ef7b~mv2.png/amazon_PNG24.png#originWidth=1024&originHeight=346",
      "link-items-all": "/items/",
      "link-items-title": "/items/2-pack-stadium-chairs-for-bleachers-with-back-support"
    },

and this is my dart code

  void getAmazon() async {
    var response = await http.get(Uri.parse(url));
    var decodeResponse = jsonDecode(response.body);
    List data = decodeResponse['items'] as List;
    Iterable filteredData = data.where((element) => element['brands'][0] == 'Amazon');

    print(filteredData); // returns nothing
  }

it doesn't return/print anything. What am I doing wrong?

CodePudding user response:

Better to use contains to check if a brand is listed. Also check if "brands" field is available for better stability.

    final filteredData = data.where((element) => (element['brands'] != null ? element['brands'].contains('Amazon') : false));

CodePudding user response:

In your code, you are checking if brands it's equal to Amazon, but brands is actually a List. (Or in the case you are checking on a particular index, this could change)

So ["Amazon"]Amazon.

In the code below you will now check if brands contains "Amazon".

Iterable filteredData = data.where((element) => element['brands'].contains('Amazon'));

CodePudding user response:

void getAmazon() async {
  var response = await http.get(Uri.parse('https://mockbin.org/bin/e123b53d-6e35-49e7-a94e-f49554a63d7e'));
  var decodeResponse = jsonDecode(response.body);
  List data = decodeResponse['items'] as List;
  Iterable filteredData = data.where((element) => element['brands'][0] == 'Amazon');

  log('ddf ${filteredData}'); // returns nothing
}

I had added third product as brand from flipkart it filter this! you can check this url https://mockbin.org/bin/e123b53d-6e35-49e7-a94e-f49554a63d7e Your code actually works as expected!!!

[log] ddf ({_id: 30baa1ca-4186-4ff0-abe8-a5970e753444, _owner: 1d3480e5-0eda-47ef-8406-38d89bf15ded, _createdDate: 2022-05-09T08:47:29.137Z, discountedPrice: 44.97, _updatedDate: 2022-05-09T08:48:44.147Z, getDealLink: https://amzn.to/3FqBq4O, brands: [Amazon], title: Mellanni Extra Deep Pocket Twin XL Sheet Set , amazonlogo: wix:image://v1/1d3480_ffad681242174f799ddea471e649ef7b~mv2.png/amazon_PNG24.png#originWidth=1024&originHeight=346, save: #1 Best Seller, link-items-all: /items/, link-items-title: /items/mellanni-extra-deep-pocket-twin-xl-sheet-set-}, {_id: a7d3aaa8-9654-4535-b6c5-b147ff0d8eb3, _owner: 1d3480e5-0eda-47ef-8406-38d89bf15ded, _createdDate: 2022-05-08T22:35:38.398Z, discountedPrice: $81.59, _updatedDate: 2022-05-08T22:39:52.801Z, getDealLink: https://amzn.to/3ymXGLe, brands: [Amazon], originalPrice: $199.99, title: 2 Pack Stadium chairs for bleachers with back support, amazonlogo: wix:image://v1/1d3480_ffad681242174f799ddea471e649ef7b~mv2.png/amazon_PNG24.png#originWidth=1024&originHeight=346, link-items-all: /items/, link-items-title: /items/2-pack-stadium-chairs-for-bleachers-with-back-support})
  • Related