PHPackages                             adinan-cenci/json-lines - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. adinan-cenci/json-lines

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

adinan-cenci/json-lines
=======================

A library to read and write json-lines.

v3.2.1(5d ago)067↓100%[1 issues](https://github.com/adinan-cenci/json-lines/issues)MITPHPPHP &gt;=7.0

Since Feb 7Pushed 5d ago1 watchersCompare

[ Source](https://github.com/adinan-cenci/json-lines)[ Packagist](https://packagist.org/packages/adinan-cenci/json-lines)[ RSS](/packages/adinan-cenci-json-lines/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (2)Versions (17)Used By (0)

Json lines
==========

[](#json-lines)

A library to read and write files in the [json lines](https://jsonlines.org/) format.

Instantiating
-------------

[](#instantiating)

```
use AdinanCenci\JsonLines\JsonLines;

$associative = true;
$file = new JsonLines('my-file.jsonl', $associative);
```

`$associative`: It will render the entries as objects if `false` and as associative arrays if `true`, it defaults to `false`.

Iterating
---------

[](#iterating)

```
foreach ($file->objects as $line => $object) {
    echo $object->myProperty . '';
    // or $object['myProperty'] if ::$associative is true.
}
```

Editing
-------

[](#editing)

### Add an object to the end of the file

[](#add-an-object-to-the-end-of-the-file)

```
$object = ['foo' => 'bar'];
$file->addObject($object);
```

`$object` does not need to be an array, it also may be an actual object.

### Add an object to the middle of the file

[](#add-an-object-to-the-middle-of-the-file)

```
$line = 5;
$object = ['foo' => 'bar'];
$file->addObject($object, $line);
```

If the file has less than `$line` lines, the gap will be filled with blank lines.

### Add several objects to the end of the file

[](#add-several-objects-to-the-end-of-the-file)

```
$objects = [
// line => object
    0 => ['name' => 'foo'],
    5 => ['name' => 'bar'],
];

$objects->addObjects($objects);
```

### Add several objects in the middle of the file

[](#add-several-objects-in-the-middle-of-the-file)

```
$objects = [
// line => object / array
    2 => ['name' => 'foo'],
    6 => ['name' => 'bar'],
];

$objects->addObjects($objects, false);
```

### Set an object

[](#set-an-object)

```
$line   = 10;
$object = ['foo' => 'bar'];
$file->setObject($line, $object);
```

The difference between `::addObject()` and `::setObject()` is that `::setObject()` will overwrite whatever is already present at `$line`.

### Set multiple objects

[](#set-multiple-objects)

```
$objects = [
// line => object / array
    0 => ['name' => 'foo'],
    5 => ['name' => 'bar'],
];

$objects->setObjects($objects);
```

### Retrieving object

[](#retrieving-object)

```
$line   = 10;
$object = $file->getObject($line);
```

Returns `null` if the entry does not exist or if the json is invalid.

### Retrieving multiple objects

[](#retrieving-multiple-objects)

```
$lines   = [0, 1, 2];
$objects = $file->getObjects($lines);
```

### Delete objects

[](#delete-objects)

```
$line = 10;
$file->deleteObject($line);
```

### Delete multiple objects

[](#delete-multiple-objects)

```
$lines = [0, 1, 2];
$file->deleteObjects($lines);
```

Searching
---------

[](#searching)

The library also provides a way to query the file.
Instantiate a new `Search` object, give it conditions and call the `::find()` method, it will return an array of matching objects indexed by their line in the file.

```
$search = $file->search();
$search->condition("object's property", 'value to compare', 'operator');
$results = $search->find();
```

### Is null operator

[](#is-null-operator)

```
$search->condition('title', null, 'IS NULL');
// Will match entries where the "title" property equals null or is
// not defined.
```

### Equals operator

[](#equals-operator)

```
$search->condition('title', 'Iliad', '=');
// Will match entries where the "title" property equals "Iliad"
// ( case insensitive ).
```

### In operator

[](#in-operator)

```
$search->condition('title', ['Iliad', ' Odyssey'], 'IN');
// Will match entries where the "title" property equals to either
// "Iliad" or "Odyssey" ( case insensitive ).
```

### Like operator

[](#like-operator)

```
$search->condition('title', 'foo', 'LIKE');
// Will match entries where the "title" property contains the word "foo"
// e.g: "foo", "foo bar", "foofighters" etc ( case insensitive ).

$search->condition('title', ['foo', 'bar'], 'LIKE');
// It also accept arrays. This will match match
// "fool", "barrier", "barista" etc.
```

### Regex operator

[](#regex-operator)

```
$search->condition('rating', '#\d stars?#', 'REGEX');
// Will match entries where the "rating" property matching "#\d stars?#"
// e.g: "1 star", "2 star", "3 stars" etc ( case insensitive ).
```

### Number comparison operators

[](#number-comparison-operators)

It also supports "less than", "greater than", "less than or equal", "greater than or equal" and "between".

```
$search
  ->condition('year', 2022, '')
  ->condition('age', 60, '=')
  ->condition('price', [10, 50], 'BETWEEN');
```

### Negating operators

[](#negating-operators)

You may also negate the operators.

```
$search
  ->condition('title', 'Iliad', '!=') // Different to ( case insensitive ).
  ->condition('title', ['Iliad', ' Odyssey'], 'NOT IN') // case insensitive.
  ->condition('price', [10, 50], 'NOT BETWEEN')
  ->condition('title', ['foo', 'bar'], 'UNLIKE');
```

### Multiple conditions

[](#multiple-conditions)

You may add multiple conditions to a search. By default all of the conditions must be met.

```
$search = $file->search();
$search
  ->condition('band', 'Iron Maiden', '=')
  ->condition('release', 2000, '');

$results = $search->find();
// Will match entries for Angra from before 2010 OR
// entries for Almah from after 2013
```

### Order

[](#order)

You may also order the results by different properties.

```
$search = $file->search();

$search->orderBy('title', 'ASC');
// Order search results by the title property alphabetically.
$search->orderBY('description', 'DESC');
// Order results by the description decrescently.
```

License
-------

[](#license)

MIT

How to install it
-----------------

[](#how-to-install-it)

Use composer.

```
composer require adinan-cenci/json-lines

```

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance99

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~124 days

Recently: every ~315 days

Total

13

Last Release

5d ago

Major Versions

v0.3.0 → v1.0.02022-05-30

v1.0.1 → v2.0.02022-09-29

v2.0.2 → v3.0.02023-07-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/3c1eb1b039e965bb307f010fb1dd41fbc79c8c9e9a65324b8f18d2bb9168097d?d=identicon)[AdinanCenci](/maintainers/AdinanCenci)

---

Top Contributors

[![adinan-cenci](https://avatars.githubusercontent.com/u/1629643?v=4)](https://github.com/adinan-cenci "adinan-cenci (45 commits)")

---

Tags

jsonJSON Lines

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/adinan-cenci-json-lines/health.svg)

```
[![Health](https://phpackages.com/badges/adinan-cenci-json-lines/health.svg)](https://phpackages.com/packages/adinan-cenci-json-lines)
```

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k311.5M536](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k463.5M118](/packages/mtdowling-jmespathphp)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k133.8M789](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k88.3M577](/packages/jms-serializer-bundle)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30421.2M37](/packages/colinodell-json5)[clue/ndjson-react

Streaming newline-delimited JSON (NDJSON) parser and encoder for ReactPHP.

15063.4M13](/packages/clue-ndjson-react)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
