Home > Software design >  Flutter get unique data from json file
Flutter get unique data from json file

Time:10-08

I have json file including more than 3000 rows and I managed to get 899 rows that I needed but also I need to filter this 899 which are in 17 chapters. So my final result would be 17.

Note: I am aware my following code is a mess and that's why I'm seeking help here.

Code

readDataBase() async {
    String data = await DefaultAssetBundle.of(context).loadString("assets/db/tb.json");
    final jsonResult = jsonDecode(data);
    var allItems = jsonResult['Passage'];

    var onlyItems = []; //gets 899 rows from  3000 rows, based on book names
    for(var i = 0; i<allItems.length; i  ) {
      if(allItems[i]['book'] == shortName){
        onlyItems.add(allItems[i]);
      }
    }
    print('onlyItems length::: ${onlyItems.length}'); //899

    // NEW FILTER GOES HERE...

    return onlyItems;
  }

Now I need to filter my 899 rows into 17 rows which is their chapters count.

Each row has object named "chapter" with ID as value (I need this filter to work based on those chapter ids)

Here is a sample of my JSON file

{
  "Passage": [
    {
      "id": "Lev-26-1-t",
      "content": "Berkat",
      "book": "Lev",
      "chapter": 26, // I need to filter my 899 rows based on this value
      "verse": 1,
      "type": "t",
      "order": 1,
      "book_name": ""
    },
    {
      "id": "Lev-26-1-c",
      "content": "\"Janganlah kamu membuat berhala bagimu, dan patung atau tugu berhala janganlah kamu dirikan bagimu; juga batu berukir janganlah kamu tempatkan di negerimu untuk sujud menyembah kepadanya, sebab Akulah TUHAN, Allahmu.",
      "book": "Lev",
      "chapter": 26, // I need to filter my 899 rows based on this value
      "verse": 1,
      "type": "c",
      "order": 2,
      "book_name": ""
    },
    .....

Note: The result I'm looking for is an integer 17 I don't need any more details of my json file, all I want is count of my unique chapters.

CodePudding user response:

I would first project the map into a list of unique integers (the chapters ids) and then take the length of this list.

Here is this part:

json["Passage"]!.fold<List<int>>(
  [],
  (previousValue, element) {
    final chapterId = element["chapter"]! as int;
    return previousValue.contains(chapterId)
        ? previousValue
        : (previousValue   [chapterId]);
  },
).length,

Here is an executable example yielding the result 2:

void main() {
  final json = {
    "Passage": [
      {
        "id": "Lev-26-1-t",
        "content": "Berkat",
        "book": "Lev",
        "chapter": 26, // I need to filter my 899 rows based on this value
        "verse": 1,
        "type": "t",
        "order": 1,
        "book_name": ""
      },
      {
        "id": "Lev-26-1-c",
        "content":
            "\"Janganlah kamu membuat berhala bagimu, dan patung atau tugu berhala janganlah kamu dirikan bagimu; juga batu berukir janganlah kamu tempatkan di negerimu untuk sujud menyembah kepadanya, sebab Akulah TUHAN, Allahmu.",
        "book": "Lev",
        "chapter": 26, // I need to filter my 899 rows based on this value
        "verse": 1,
        "type": "c",
        "order": 2,
        "book_name": ""
      },
      {
        "id": "Lev-26-1-c",
        "content":
            "\"Janganlah kamu membuat berhala bagimu, dan patung atau tugu berhala janganlah kamu dirikan bagimu; juga batu berukir janganlah kamu tempatkan di negerimu untuk sujud menyembah kepadanya, sebab Akulah TUHAN, Allahmu.",
        "book": "Lev",
        "chapter": 27, // I need to filter my 899 rows based on this value
        "verse": 1,
        "type": "c",
        "order": 2,
        "book_name": ""
      },
    ],
  };

  print(
    json["Passage"]!.fold<List<int>>(
      [],
      (previousValue, element) {
        final chapterId = element["chapter"]! as int;
        return previousValue.contains(chapterId)
            ? previousValue
            : (previousValue   [chapterId]);
      },
    ).length,
  ); // 2
}
  • Related