Flutter json folder structure and search hierachy

Issue

I have a nested json file.

[
  {
    "name": "Purchases of goods",
    "key": "f1",
    "type":  "folder",
    "owner": "admin",
    "subfolders": [
      {
        "name": "Invoice 1",
        "key": "f28",
        "type":  "file",
        "owner": "user",
        "filename": "inv1.png"
      },
      {
        "name": "Invoice 2",
        "key": "f29",
        "type":  "file",
        "owner": "user",
        "filename": "inv2.jpg"
      },
      {
        "name": "Invoice 3",
        "key": "f30",
        "type":  "file",
        "owner": "user",
        "filename": "inv3.png"
      },
      {
        "name": "Invoice 4",
        "key": "f31",
        "type":  "file",
        "owner": "user",
        "filename": "inv4.png"
      },
      {
        "name": "Invoice 5",
        "key": "f32",
        "type":  "file",
        "owner": "user",
        "filename": "inv5.jpg"
      },
      {
        "name": "Invoice 6",
        "key": "f33",
        "type":  "file",
        "owner": "user",
        "filename": "inv6.png"
      },
      {
        "name": "Invoice 7",
        "key": "f34",
        "type":  "file",
        "owner": "user",
        "filename": "inv7.png"
      }
    ]
  },
  {
    "name": "Staff costs",
    "key": "f2",
    "type":  "folder",
    "owner": "admin",
    "subfolders": [
      {
        "key": "f3",
        "name": "Salaries",
        "type":  "folder",
        "owner": "admin",
        "subfolders": [
          {
            "name": "Invoice 1",
            "key": "f35",
            "type":  "file",
            "owner": "user",
            "filename": "inv1.png"
          },
          {
            "name": "Invoice 2",
            "key": "f36",
            "type":  "file",
            "owner": "user",
            "filename": "inv2.jpg"
          },
          {
            "name": "Invoice 3",
            "key": "f37",
            "type":  "file",
            "owner": "user",
            "filename": "inv3.png"
          },
          {
            "name": "Invoice 4",
            "key": "f38",
            "type":  "file",
            "owner": "user",
            "filename": "inv4.png"
          },
          {
            "name": "Invoice 5",
            "key": "f39",
            "type":  "file",
            "owner": "user",
            "filename": "inv5.jpg"
          },
          {
            "name": "Invoice 6",
            "key": "f40",
            "type":  "file",
            "owner": "user",
            "filename": "inv6.png"
          },
          {
            "name": "Invoice 7",
            "key": "f41",
            "type":  "file",
            "owner": "user",
            "filename": "inv7.png"
          }
        ]
      },
      {
        "key": "f4",
        "name": "Social insurance benefits",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f5",
        "name": "AVS, AI, APG, unemployment insurance",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f6",
        "name": "Occupational pension",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f7",
        "name": "Accident insurance",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f8",
        "name": "Daily allowance insurance in the event of illness",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f9",
        "name": "Temporary employees",
        "type":  "folder",
        "owner": "admin"
      }
    ]
  },
  {
    "name": "RENTS",
    "key": "f10",
    "type":  "folder",
    "owner": "admin"
  },
  {
    "name": "Vehicle costs",
    "key": "f11",
    "type":  "folder",
    "owner": "admin",
    "subfolders": [
      {
        "key": "f12",
        "name": "Repair, service and cleaning",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f13",
        "name": "Fuels",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f14",
        "name": "Insurance and taxes",
        "type":  "folder",
        "owner": "admin"
      }
    ]
  },
  {
    "name": "INSURANCE",
    "key": "f15",
    "type":  "folder",
    "owner": "admin"
  },
  {
    "name": "ADMINISTRATIVE CHARGES",
    "key": "f16",
    "type":  "folder",
    "owner": "admin",
    "subfolders": [
      {
        "key": "f17",
        "name": "Office supplies",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f18",
        "name": "Phone",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f19",
        "name": "Internet",
        "type":  "folder",
        "owner": "admin"
      }
    ]
  },
  {
    "name": "AMORTIZATION",
    "key": "f20",
    "type":  "folder",
    "owner": "admin",
    "subfolders": [
      {
        "key": "f21",
        "name": "Machine and tool depreciation",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f22",
        "name": "Furniture depreciation",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f23",
        "name": "Depreciation of office machines, IT",
        "type":  "folder",
        "owner": "admin"
      },
      {
        "key": "f24",
        "name": "Vehicle depreciation",
        "type":  "folder",
        "owner": "admin"
      }
    ]
  },
  {
    "name": "Computers",
    "key": "f25",
    "type":  "folder",
    "owner": "user",
    "subfolders": [
      {
        "name": "Invoice one",
        "key": "f26",
        "type":  "file",
        "owner": "user",
        "filename": "inv1.png"
      },
      {
        "name": "Invoice two",
        "key": "f27",
        "type":  "file",
        "owner": "user",
        "filename": "inv2.jpg"
      }
    ]
  },
  {
    "name": "Invoice one",
    "key": "f26",
    "type":  "file",
    "owner": "user",
    "filename": "inv1.png"
  },
  {
    "name": "Invoice two",
    "key": "f27",
    "type":  "file",
    "owner": "user",
    "filename": "inv2.jpg"
  }
]

I have listed this files to the app using flutter grid view. it working fine. Now i have a problem with search the json.

eg:

if you search for the "key": "f35",
it comes under Staff costs -> Salaries -> Invoice 1 This is the output i want. I want to find the parents hierachy of the json. I am not sure how to implement this. I want to just print the value if i search for the key

Solution

loadJson() async{
    String data = await rootBundle.loadString('assets/json/expenses/en.json');
    data = data.toString();
    final jsonResult = json.decode(data);
    //print(jsonResult);
    String keyValue = "f1";
    var parentFolders = [];
    var folderPath = '';
    var pathInfo = folderList(jsonResult, parentFolders, keyValue, folderPath);
  }

folderList(List folders, parentFolders, keyValue, folderPath) {
    for (var i = 0; i < folders.length; i++)
    {
       if(folders[i].containsKey("subfolders")) {
         parentFolders.add(folders[i]["name"]);
         if(folders[i]["key"]==keyValue) {
           // print("match 1");
           // print(parentFolders.join(' / '));
           folderPath = parentFolders.join(' / ');
           return folderPath;
         }
         folderPath = folderList(folders[i]["subfolders"], parentFolders, keyValue, folderPath);
         parentFolders.removeLast();
       }
       else{
         if(folders[i]["key"]==keyValue) {
           // print("match 2");
           // print(parentFolders.join(' / '));
           folderPath = parentFolders.join(' / ');
           return folderPath;
         }
       }
    }
    return folderPath;
  }

Answered By – Soundhar Raj

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.