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...
}