The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started


Inserting Records

Selecting Records

Converting Records

Upgrading Guide

This is the documentation for the upcoming version 9.0. This is a work in progress

JSON conversion

JsonConverter converts a CSV records collection into a JSON string by implementing the Converter interface.




public JsonConverter::options(int options): self

This method sets PHP’s json_encode optional flags.


public JsonConverter::convert(iterable $records): string

The JsonConverter::convert accepts an iterable which represents the records collection and returns a string.

If a error occurs during the convertion an RuntimeException exception is thrown with additional information regarding the error.


use League\Csv\JsonConverter;
use League\Csv\Reader;

$csv = Reader::createFromPath('/path/to/file.csv', 'r')

$json = (new JsonConverter())

echo '<pre>', $json, PHP_EOL;
// [
//     {
//         "firstname": "john",
//         "lastname": "doe",
//         "email": ""
//     },
//     {
//         "firstname": "jane",
//         "lastname": "doe",
//         "email": ""
//     },
//     ...
//     {
//         "firstname": "san",
//         "lastname": "goku",
//         "email": "san.goku@dragon-ball.super"
//     }
// ]

Tip: if needed you can use the Encoder object to correctly encode your CSV records before conversion.