Hi, I am trying to convert a json file with a different array structure than normal to a csv file. I tried to find a solution to convert it to a csv file, but I can not find a solution. <?php
$jsondata = '[{
"accession_number_original": "2012.11.45",
"author_birth_date": [
"1932"
],
"author_date": [
"1932"
],
"author_death_date": [
""
],
"author_description": [
"American"
],
"author_display": [
"Day yon"
],
"author_names_first": [
"Day"
],
"author_names_last": [
"yon"
],
"author_names_middle": [
""
],
"image_height": "12 1/2",
"image_width": "18 1/4",
"jp2_image_url": "",
"location_physical_location": "Art Gallery",
"location_shelf_locator": "Unknown",
"master_image_url": "",
"note_provenance": "Gift of Gary Ginsberg and Susanna Aaron",
"object_date": "1963/2010",
"object_depth": "",
"object_height": "",
"object_width": "",
"origin_datecreated_end": "1963",
"origin_datecreated_start": "1963",
"physical_description_extent": [
"12 1/2 x 18 1/4"
],
"physical_description_material": [
"Gelatin silver print"
],
"physical_description_technique": [
"Gelatin silver print"
],
"pid": "bdr:123456",
"title": "As Demonstrators"
}]';
$jsonDecoded = json_decode($jsondata);
print_r('<pre>');
print_r($jsonDecoded);
print_r('</pre>');
$fh = fopen('fileout.csv', 'w');
if (is_array($jsonDecoded)){
print_r('<-------- line variable output-------->');
foreach($jsonDecoded as $line){
print_r('<pre>'); print_r($line); print_r('</pre>');
print_r('<-------- data variable output-------->');
if (is_array($line)||is_object($line)){
foreach($line as $data){
fputcsv($fp,$data);
}
}
}
}
}
fclose($fh);
print_r('Converted Successfully');
?>
I tried to study most of these issues in stackoverflow, but none of them have an array that is not very useful to me.
If I use a single foreach, I get an Array to String Conversion failed error message and the Array is printed as a value instead of the actual data in the csv file.
If I use two foreach, I get an error fputcsv () expects parameter 2 to be set as an array string
The result of var_dump or print_r of decoded json is as follows
Array
(
[0] => stdClass Object
(
[accession_number_original] => 2012.11.45
[author_birth_date] => Array
(
[0] => 1932
)
[author_date] => Array
(
[0] => 1932
)
[author_death_date] => Array
(
[0] =>
)
[author_description] => Array
(
[0] => American
)
[author_display] => Array
(
[0] => Day yon
)
[author_names_first] => Array
(
[0] => Day
)
[author_names_last] => Array
(
[0] => yon
)
[author_names_middle] => Array
(
[0] =>
)
[image_height] => 12 1/2
[image_width] => 18 1/4
[jp2_image_url] =>
[location_physical_location] => Art Gallery
[location_shelf_locator] => Unknown
[master_image_url] =>
[note_provenance] => Gift of Gary Ginsberg and Susanna Aaron
[object_date] => 1963/2010
[object_depth] =>
[object_height] =>
[object_width] =>
[origin_datecreated_end] => 1963
[origin_datecreated_start] => 1963
[physical_description_extent] => Array
(
[0] => 12 1/2 x 18 1/4
)
[physical_description_material] => Array
(
[0] => Gelatin silver print
)
[physical_description_technique] => Array
(
[0] => Gelatin silver print
)
[pid] => bdr:123456
[title] => As Demonstrators
)
)