Giter Site home page Giter Site logo

ndjson's Introduction

NDJSON Reader/Writer for PHP

Latest License PHP Test codecov


A PHP library to read and write NDJSON (Newline Delimited JSON).

Read and write one line at a time to execute with low memory usage.

For better performance, you can also read and write on multiple lines.

Installation

composer require sunaoka/ndjson

Usage

Read

Example NDJSON

{"test": "001"}
{"test": "002"}
{"test": "003"}
{"test": "004"}
{"test": "005"}

Read one line at a time

use Sunaoka\Ndjson\NDJSON;

$ndjson = new NDJSON('/path/to/file.ndjson');

while ($json = $ndjson->readline()) {
    var_dump($json);
}
array(1) {
  ["test"]=>
  string(3) "001"
}
array(1) {
  ["test"]=>
  string(3) "002"
}
array(1) {
  ["test"]=>
  string(3) "003"
}
array(1) {
  ["test"]=>
  string(3) "004"
}
array(1) {
  ["test"]=>
  string(3) "005"
}

Read 3 lines at a time

use Sunaoka\Ndjson\NDJSON;

$ndjson = new NDJSON('/path/to/file.ndjson');

foreach ($ndjson->readlines(3) as $jsons) {
    var_dump($jsons);
}
array(3) {
  [0]=>
  array(1) {
    ["test"]=>
    string(3) "001"
  }
  [1]=>
  array(1) {
    ["test"]=>
    string(3) "002"
  }
  [2]=>
  array(1) {
    ["test"]=>
    string(3) "003"
  }
}
array(2) {
  [0]=>
  array(1) {
    ["test"]=>
    string(3) "004"
  }
  [1]=>
  array(1) {
    ["test"]=>
    string(3) "005"
  }
}

Write

Write one line at a time

use Sunaoka\Ndjson\NDJSON;

$ndjson = new NDJSON('/path/to/file.ndjson');
$ndjson->writeline(['test' => '001']);
$ndjson->writeline(['test' => '002']);
{"test": "001"}
{"test": "002"}

Write multiple lines at a time

use Sunaoka\Ndjson\NDJSON;

$ndjson = new NDJSON('/path/to/file.ndjson');
$ndjson->writelines([['test' => '001'], ['test' => '002']]);
{"test": "001"}
{"test": "002"}

ndjson's People

Contributors

sunaoka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.