1

We are having array of json objects like below:

[{
    "CompanyCode": "1",
    "MachineCode": null,
    "MachineName": null,
    "NoOfMeters": null,
    "ScheduleNo": "12028",
    "ScheduleDate": "17/2/2018 12:00:00 AM",
    "ScheduleUser": "sales",
    "CustomerCode": "0022100007",
    "DeliveryCode": "0",
    "SortOrder": "1",
    "JobRefNo": "",
    "CustomerName": "COLD STORAGE SUPERMARKETS ",
    "CustAddress1": "GIANT COMPLEX",
    "OutletName": "",
    "OutAddress1": "",
    "IsJobClosed": ""
}, {
    "CompanyCode": "1",
    "MachineCode": null,
    "MachineName": null,
    "NoOfMeters": null,
    "ScheduleNo": "12029",
    "ScheduleDate": "17/2/2018 12:00:00 AM",
    "ScheduleUser": "sales",
    "CustomerCode": "0022100008",
    "DeliveryCode": "0",
    "SortOrder": "1",
    "JobRefNo": "",
    "CustomerName": "COLD STORAGE WH - 21 TAMPINES ",
    "CustAddress1": "GIANT COMPLEX",
    "OutletName": "",
    "OutAddress1": "",
    "IsJobClosed": ""
}, {
    "CompanyCode": "1",
    "MachineCode": null,
    "MachineName": null,
    "NoOfMeters": null,
    "ScheduleNo": "12027",
    "ScheduleDate": "17/2/2018 12:00:00 AM",
    "ScheduleUser": "winapp",
    "CustomerCode": "0022100003",
    "DeliveryCode": "0",
    "SortOrder": "1",
    "JobRefNo": "",
    "CustomerName": "AVENZA PTE LTD ",
    "CustAddress1": "83 CLEMENCEAU AVE",
    "OutletName": "",
    "OutAddress1": "",
    "IsJobClosed": ""
}, {
    "CompanyCode": "1",
    "MachineCode": null,
    "MachineName": null,
    "NoOfMeters": null,
    "ScheduleNo": "12025",
    "ScheduleDate": "17/2/2018 12:00:00 AM",
    "ScheduleUser": "winapp",
    "CustomerCode": "0022100001",
    "DeliveryCode": "0",
    "SortOrder": "1",
    "JobRefNo": "",
    "CustomerName": "CASH ON DELIVERY (TO) ",
    "CustAddress1": "CASH ON DELIVERY",
    "OutletName": "",
    "OutAddress1": "",
    "IsJobClosed": ""
}, {
    "CompanyCode": "1",
    "MachineCode": null,
    "MachineName": null,
    "NoOfMeters": null,
    "ScheduleNo": "12026",
    "ScheduleDate": "17/2/2018 12:00:00 AM",
    "ScheduleUser": "winapp",
    "CustomerCode": "0022100002",
    "DeliveryCode": "0",
    "SortOrder": "1",
    "JobRefNo": "",
    "CustomerName": "DESPATCH - MT ",
    "CustAddress1": "DESPATCH",
    "OutletName": "",
    "OutAddress1": "",
    "IsJobClosed": ""
}]

We wanted group this array based on ScheduleUser key like section.

[
  "Sales":[
            {

            }
            {

            }

           ]
  "winapp":[
            {

                }
            {

                }

            {

            }

            ]
  ]

We have studied about grouping in swift 4 Here

But they given with array of string, we don't have idea to working json values. Kindly give us solution if any other way. we have referred following Link

We are not able to understand this.

Getting values from local

if let path = Bundle.main.path(forResource: "salesorder", ofType: "json") {
        do {
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
            do{
                let json =  try JSONSerialization.jsonObject(with: data, options: .allowFragments)
                let jsonDictionary =  json as? [[String:Any]]

                print(jsonDictionary!)

            }catch let error{
                print(error.localizedDescription)
            }
        } catch let error {
            print(error.localizedDescription)
        }
    } else {
        print("Invalid filename/path.")
    }

Thanks in advance..

karthikeyan
  • 3,821
  • 3
  • 22
  • 45

2 Answers2

1

Better

Using grouping way

if let path = Bundle.main.path(forResource: "salesorder", ofType: "json") {
        do {
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
            do{
                let json =  try JSONSerialization.jsonObject(with: data, options: .allowFragments)
                if let jsonDictionary =  json as? [[String:Any]]{
                
                var grouped2 : [String:[[String:Any]]] = Dictionary(grouping: jsonDictionary, by: {$0["ScheduleUser"] as! String})
                debugPrint(grouped2)
                print(jsonDictionary)
                }
                
            }catch let error{
                print(error.localizedDescription)
            }
        } catch let error {
            print(error.localizedDescription)
        }
    } else {
        print("Invalid filename/path.")
    }

Manual way

if let path = Bundle.main.path(forResource: "salesorder", ofType: "json") {
            do {
                let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
                do{
                    let json =  try JSONSerialization.jsonObject(with: data, options: .allowFragments)
                    if let jsonDictionary =  json as? [[String:Any]]{
                    
                        var grouped : [String:[[String:Any]]] = [:]
                        for dict in jsonDictionary {
                            let value = dict["ScheduleUser"] as! String
                            if(grouped[dict["ScheduleUser"] as! String] == nil){
                                grouped[value] = [dict]
                            }else{
                                grouped[value]?.append(dict)
                            }
                        }
                        debugPrint(grouped)
                    print(jsonDictionary)
                    }
                    
                }catch let error{
                    print(error.localizedDescription)
                }
            } catch let error {
                print(error.localizedDescription)
            }
        } else {
            print("Invalid filename/path.")
        }

Log

["winapp": [["MachineName": , "CustomerCode": 0022100003, "JobRefNo": , "OutAddress1": , "SortOrder": 1, "IsJobClosed": , "CustAddress1": 83 CLEMENCEAU AVE, "OutletName": , "MachineCode": , "ScheduleDate": 17/2/2018 12:00:00 AM, "CustomerName": AVENZA PTE LTD , "ScheduleNo": 12027, "NoOfMeters": , "DeliveryCode": 0, "ScheduleUser": winapp, "CompanyCode": 1], ["MachineName": , "CustomerCode": 0022100001, "JobRefNo": , "OutAddress1": , "SortOrder": 1, "IsJobClosed": , "CustAddress1": CASH ON DELIVERY, "OutletName": , "MachineCode": , "ScheduleDate": 17/2/2018 12:00:00 AM, "CustomerName": CASH ON DELIVERY (TO) , "ScheduleNo": 12025, "NoOfMeters": , "DeliveryCode": 0, "ScheduleUser": winapp, "CompanyCode": 1], ["MachineName": , "CustomerCode": 0022100002, "JobRefNo": , "OutAddress1": , "SortOrder": 1, "IsJobClosed": , "CustAddress1": DESPATCH, "OutletName": , "MachineCode": , "ScheduleDate": 17/2/2018 12:00:00 AM, "CustomerName": DESPATCH - MT , "ScheduleNo": 12026, "NoOfMeters": , "DeliveryCode": 0, "ScheduleUser": winapp, "CompanyCode": 1]], "sales": [["MachineName": , "CustomerCode": 0022100007, "JobRefNo": , "OutAddress1": , "SortOrder": 1, "IsJobClosed": , "CustAddress1": GIANT COMPLEX, "OutletName": , "MachineCode": , "ScheduleDate": 17/2/2018 12:00:00 AM, "CustomerName": COLD STORAGE SUPERMARKETS , "ScheduleNo": 12028, "NoOfMeters": , "DeliveryCode": 0, "ScheduleUser": sales, "CompanyCode": 1], ["MachineName": , "CustomerCode": 0022100008, "JobRefNo": , "OutAddress1": , "SortOrder": 1, "IsJobClosed": , "CustAddress1": GIANT COMPLEX, "OutletName": , "MachineCode": , "ScheduleDate": 17/2/2018 12:00:00 AM, "CustomerName": COLD STORAGE WH - 21 TAMPINES , "ScheduleNo": 12029, "NoOfMeters": , "DeliveryCode": 0, "ScheduleUser": sales, "CompanyCode": 1]]]

Community
  • 1
  • 1
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
1

Get all possible values in an array -

let arr = your data...
let posValues = arr.map { $0["ScheduleUser"] as? String }

Now create a dictionary for filter data -

var newDict:[String:Any] = [:]

for aValue in posValues {

    let filteredDicts = arr.filter({ ($0.["ScheduleUser"] == aValue)})
    newDict[aValue] = filteredDicts
}
Blind Ninja
  • 1,063
  • 13
  • 28