-2

how to get this json from a csv file. The csv file has the headers:

Description, BusinessSurname, IsCustomer, IsSupplier, AddressType, BusinessAddress, IsInternational

And First row:

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

I need that data in the csv to convert to that exact json, but need it to loop for any other rows that may exist.

{
Description:'Contact1',
SurnameBusinessName:'Contact1',
IsCustomer:True,
IsSupplier:True,
Addresses:
[
{AddressType:'Business',Line1:'123 Fake St',IsInternational:False},
]
}

I cant make it work. please help.

########################################

I found the answer to this, after trying every possible solution I realised I had to manually create the json as no json or array would give me exactly what I wanted. 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; 

1 Answers1

0

Script

[akshay@localhost tmp]$ cat test.php
<?php

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 0, $delimiter)) !== FALSE)
        {

            if(!$header)
            {
               // if you want keys with space char then
               // $header = $row; 
               // I prefer trimming
               $header = array_map('trim',$row);
            }
            else
            {
                if(count($header)!=count($row)){ continue; }

                $data[] = array_combine($header, $row);
            }
        }
        fclose($handle);
    }
    return $data;
}

print_r(json_encode(csv_to_array("/tmp/test.csv"),JSON_PRETTY_PRINT));

?>

Sample csv file

[akshay@localhost tmp]$ cat test.csv
Description, BusinessSurname, IsCustomer, IsSupplier, AddressType, BusinessAddress, IsInternational
Contact1, Contact1, True, True, Business, 123 Fake St, False

Execution

[akshay@localhost tmp]$ php test.php
[
    {
        "Description": "Contact1",
        "BusinessSurname": " Contact1",
        "IsCustomer": " True",
        "IsSupplier": " True",
        "AddressType": " Business",
        "BusinessAddress": " 123 Fake St",
        "IsInternational": " False"
    }
]
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
  • I'm already at that point, but the problem i'm having is getting the address nested, which was the point of my question. Thanks anyway appreciate your response. – user8114890 Jun 28 '17 at 01:04