Khrisna Gunanasurya
Khrisna Gunanasurya

Reputation: 745

Converting JSONL to Array with PHP

I have this piece of code to get the file from AWS S3 using aws/aws-sdk-php-laravel package.

try {
    $s3Client = AWS::createClient('s3');
    $result = $s3Client->getObject([
        'Bucket'    => static::$s3Bucket,
        'Key'       => $fileName
    ]);

    return $result['Body']->getContents();
} catch (S3Exception $e) {
    return;
}

it working fine, but when I tried to using explode(PHP_EOL, $result['Body']->getContents()); it's only showing 1 array, and inside that array there will be the result from the jsonl file, so how to extract the json data from jsonl file?

Upvotes: 0

Views: 1573

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

The problem is that when you use PHP_EOL, you are using the line break for your specific platform. This may be different than the file encoding.

In the spec of JSONL...

  1. Line Separator is '\n'

So if your platform uses \r\n for end of lines (PHP_EOL) then it won't split the lines.

The solution is to always use \n for JSONL files...

explode("\n", $result['Body']->getContents());

Upvotes: 1

Related Questions