10

I've got a JSON service and need to create a script to export data to CSV files. Does anyone have a method or library you can suggest to migrate JSON to CSV format?

Here's an example format though I expect to have to retro-fit the solution to work with it:

{"service_name":
      { key : value, key : value....}
}

or:

{"service_name":
        [
               { key : value, key : value....},
               ...
         ]
}
5
  • 1
    can you do the reverse of this question? Commented Mar 5, 2012 at 20:24
  • 5
    What's the structure of the JSON? JSON can have a very complex, nested structure that may be impossible to meaningfully render as a csv. Commented Mar 5, 2012 at 20:25
  • stackoverflow.com/questions/4811844/csv-to-json-with-php Commented Mar 5, 2012 at 20:25
  • it's not as simple as just reversing those methods. thanks for the suggestions though Commented Mar 5, 2012 at 20:49
  • I'm editing the question now with the structure though I'm going to need to change the method either way to fit our service. I just want a good example Commented Mar 5, 2012 at 20:50

3 Answers 3

13

i generally agree with the commenters, but if you're data is prepared this way, isn't this pseudo-code all you need?

$json_str = "{'aintlist':[4,3,2,1], 'astringlist':['str1','str2']}";

$json_obj = json_decode ($json_str);

$fp = fopen('file.csv', 'w');

foreach ($json_obj as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
Sign up to request clarification or add additional context in comments.

1 Comment

In my case I'd to cast (array) on fields. PHP 5.3.
2

Something like this should work, assuming your JSON is an array of data sets without arrays or embedded objects:

$file = file_get_contents('http://example.com/blah/blah');
$json = json_decode($file);

$csvfile = fopen('file.csv', 'w+');
foreach ($json as $row) {
    $line = "'" . join("\",\"", $row) . "\"\n";
    fputs($csvfile, $line);
}
fclose($csvfile);

You'll have to add appropriate error handling. There are a lot of things that can go wrong when trying to do this sort of thing (i.e. JSON file not available or is malformatted, can't create new CSV file)

Comments

2

I just needed to do the same. I wrote the small command line script, which take as a parameter the json file and output the CSV.

You can check it here: PHP Converting JSON array to CSV

The important staff there is using keys of array as the first row in CSV file. And maintaining the order on the next elements, to not mess up the CSV.

Here is the code:

if (empty($argv[1])) die("The json file name or URL is missed\n");
$jsonFilename = $argv[1];

$json = file_get_contents($jsonFilename);
$array = json_decode($json, true);
$f = fopen('php://output', 'w');

$firstLineKeys = false;
foreach ($array as $line)
{
    if (empty($firstLineKeys))
    {
        $firstLineKeys = array_keys($line);
        fputcsv($f, $firstLineKeys);
        $firstLineKeys = array_flip($firstLineKeys);
    }
    // Using array_merge is important to maintain the order of keys acording to the first element
    fputcsv($f, array_merge($firstLineKeys, $line));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.