Search code examples
phparraysjsonfgetcsv

converting fgetcsv response into specific json


I have a .CSV file with headings:

Description, BusinessSurname, IsCustomer, IsSupplier, AddressType, Business 
Address, IsInternational.

First row:

Contact1, Contact1, True, True, Business, 123 Fake St, False

Remaining rows not important, its just more like that - examples. I have a few rows of data in there.

I need to get it into this format json:

{
Description:'Desc_47AE3208-87F5-4BBA-BE40-AA4130AB4768',
SurnameBusinessName:'Name_Business',
IsCustomer:true,
IsSupplier:true,
Addresses:
[
{AddressType:'Business',Line1:'addr1_bus',IsInternational:false},
{AddressType:'Postal',Line1:'addr1_pos',IsInternational:true}
]
}

I have tried a few different ways, but none of them specifically give me a json like this with the address nested. I can leave the second address (postal address) out.

If I use this code:

        $filename = 'contacts1.csv';
        $handle = fopen($filename, 'r');
        $count = 0;
        while (($data = fgetcsv($handle)) !== FALSE) {
            $count++;
            if ($count == 1){
                continue;
            }
        $json = json_encode($data, true);
        echo $json;
        };

I get this for example:

["Contact1","Contact1","TRUE","TRUE","Business","123 High Street Sydney NSW 2000","FALSE"]
["Contact2","Contact2","TRUE","TRUE","Business","124 High Street Sydney NSW 2000","FALSE"]
["Contact3","Contact3","TRUE","TRUE","Business","125 High Street Sydney NSW 2000","FALSE"]

Is there a way to get the json I need, and if I can't get the required json automatically, is there a way I can extract the values of each row, and assign to a variable, and manually create the required json for each row, using a for loop, while loop, etc? EG:

{
Description: $description,
SurnameBusinessName: $BusinessSurname,
etc...
}

Solution

  • I found the answer to this, however I had given up on this API endpoint and went to a simpler one without nesting. Due to the strictness of the json format this API will accept I basically assigned each value from my csv to a variable and manually created the json needed. Although this code below is for the Accounts endpoint, I can use the exact same method for any endpoint because I am kind of manually creating the json. And its the only way it can work I think as some fields are strings like AccountName, some integers like AcountType.

    $file = 'acc.csv';
        $mode = 'r';
        $handle = fopen($file, $mode);
        while(($csv = fgetcsv($handle)) !==FALSE){
            foreach($csv as $row => $value){
                $data = $row.$value;
                switch ($row){
                    case '0':
                        $accounttype = $value;
                        break;
                    case '1':
                        $accountname = $value;
                        break;
                    case '2':
                        $hint = $value;
                        break;
                    case '3':
                        $status = $value;
                        break;
                    case '4':
                        $sortorder = $value;
                        break;
                    case '5':
                        $accountcode = $value;
                        break;
                    case '6':
                        $parentaccountcatid = $value;
    
                        $json = "
                        {
          AccountType:" . $accounttype . ",
          AccountName:'" . $accountname . "',
          Hint:'" . $hint . "',
          Status:" . $status . ",
          SortOrder:" . $sortorder . ",
          AccountCode:'" . $accountcode . "',
          ParentAccountingCategoryID:'" . $parentaccountcatid . "'
         }"; 
        //echo $json;