user8114890 user8114890 - 1 year ago 55
PHP Question

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:


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) {
if ($count == 1){
$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,

Answer Source

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;
                case '1':
                    $accountname = $value;
                case '2':
                    $hint = $value;
                case '3':
                    $status = $value;
                case '4':
                    $sortorder = $value;
                case '5':
                    $accountcode = $value;
                case '6':
                    $parentaccountcatid = $value;

                    $json = "
      AccountType:" . $accounttype . ",
      AccountName:'" . $accountname . "',
      Hint:'" . $hint . "',
      Status:" . $status . ",
      SortOrder:" . $sortorder . ",
      AccountCode:'" . $accountcode . "',
      ParentAccountingCategoryID:'" . $parentaccountcatid . "'
    //echo $json; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download