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;