PHPackages                             gemul/jsonparser - 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. gemul/jsonparser

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

gemul/jsonparser
================

A PHP Library to easily handle JSON data

v1.0.2(2y ago)1696Apache-2.0PHPPHP &gt;=7.0.1

Since Oct 23Pushed 2y ago1 watchersCompare

[ Source](https://github.com/gemul/jsonparser)[ Packagist](https://packagist.org/packages/gemul/jsonparser)[ RSS](/packages/gemul-jsonparser/feed)WikiDiscussions main Synced yesterday

READMEChangelog (1)DependenciesVersions (4)Used By (0)

PHP JSON Parser
===============

[](#php-json-parser)

Introduction
------------

[](#introduction)

Welcome to the PHP JSON Library, a simple tool for handling JSON data in your PHP applications. This library provides a set of functions for reading JSON data from strings or files, manipulating JSON data, and conveniently handling cases where a key doesn't exist in your JSON structure by returning a default value.

Features
--------

[](#features)

- **Read JSON from String**: Easily parse JSON data from a string and work with it within your PHP application.
- **Read JSON from File**: Quickly read JSON data from a file, simplifying the process of working with external JSON files.
- **Manipulate JSON Data**: Perform various operations on your JSON data, such as adding, updating, or removing keys, all with a simple and intuitive API.
- **Default Value Handling**: Handle cases where a JSON key doesn't exist by specifying a default value, preventing unexpected errors in your code.

Installation
------------

[](#installation)

To get started, you can install the library using [Composer](https://getcomposer.org):

```
composer require gemul/jsonparser
```

Usage
-----

[](#usage)

### 1. Load JSON Data

[](#1-load-json-data)

Reading JSON from String

```
$json = new \Gemul\JsonParser($json_string);
```

or from JSON file

```
$json = new \Gemul\JsonParser(storage_path('/app/public/dummy.json'));
```

you can alson define separator string used for traversing the json tree, in constructor's second parameter (default ".").

```
$json = new \Gemul\JsonParser($json_string,'->');
```

### 2. Getting JSON data

[](#2-getting-json-data)

Given this json structure:

```
{
  "openapi": "3.0.3",
  "info": {
    "title": "Swagger Petstore - OpenAPI 3.0",
    "description": "This is a sample",
    "contact": {
      "email": "apiteam@swagger.io"
    }
  },
  "externalDocs": {
    "description": "Find out more about Swagger",
    "url": "http://swagger.io"
  },
  "servers": [
    {
      "url": "https://petstore2.swagger.io/api/v2"
    },
    {
      "url": "https://petstore3.swagger.io/api/v3"
    }
  ],
  ...
}
```

you can use `getItem(path,[default value])` to traverse the json tree and get the data.

```
$json->getItem('openapi'); // 3.0.3
$json->getItem('info.title'); // Swagger Petstore - OpenAPI 3.0
$json->getItem('info.contact.email'); // apiteam@swagger.io
$json->getItem('servers.1.url'); // https://petstore3.swagger.io/api/v3
```

it can also return the rest of the json branches, for example

```
$json->getItem('info.contact'); // stdClass {"email": "apiteam@swagger.io"}
```

#### Handling path that doesn't exist

[](#handling-path-that-doesnt-exist)

if the path doesn't exist, the default value is returned which is either null (default) or anything you put on second parameter. It won't throw 'Undefined property' exception even if the whole path doesn't exist. So you can safely traverse without having to do check at every level.

```
$json->getItem('info.contact.phone'); // null (path not found in tree, default to null)
$json->getItem('info.contact.phone',1234); // 1234 (set the default to 1234 instead of null)
$json->getItem('info.foo.bar',false); // false (doesn't throw 'Undefined property "foo"' exception)
```

### 3. Set a JSON data

[](#3-set-a-json-data)

In order to set a data to the tree, you can use `setItem(path,value)` for example

```
$json->setItem('info.version','1.0.0');
```

the json would become

```
{
  "openapi": "3.0.3",
  "info": {
    "title": "Swagger Petstore - OpenAPI 3.0",
    "description": "This is a sample",
    "contact": {
      "email": "apiteam@swagger.io"
    },
    "version": "1.0.0"
  },
  ...
```

You can safely make new depth to the path

```
$json->setItem('info.foo.bar.baz','somevalue');
```

```
{
  "openapi": "3.0.3",
  "info": {
    "foo": {
        "bar": {
            "baz": "somevalue"
        }
    },
    "title": "Swagger Petstore - OpenAPI 3.0",
  ...
```

#### Setting Array

[](#setting-array)

For array data, you can explicitly use index, or use '\[\]' to change or append an element into either existing or new array.

```
$json->setItem('servers.2.url', "new url");
//or
$json->setItem('servers.[].url', "new url");
```

will result in

```
...
  "servers": [
    {
      "url": "https://petstore2.swagger.io/api/v2"
    },
    {
      "url": "https://petstore3.swagger.io/api/v3"
    },
    {
      "url": "new url"
    }
  ],
...
```

### 3. Get last valid path

[](#3-get-last-valid-path)

After executing `getItem()`, you can use `getLastValidPath()` to retrieve the last valid path that was successfully traversed by the getItem method.

```
$json->getItem('info.contact.phone.home');
//will return "info.contact", because from 'phone' onward doesn't exist
```

### 4. Get the current full JSON object

[](#4-get-the-current-full-json-object)

To get the current json as an object, use `getJsonObject()`. Or alternatively the json-encoded string using `getJsonString()`.

### 5. Save json string to file

[](#5-save-json-string-to-file)

To save the json string, use `saveAs(file_path)`

```
$json->saveAs(storage_path('/app/public/result.json'));
```

make sure that the directory is writable.

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity40

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 ~0 days

Total

3

Last Release

985d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/07256d79aa9a59004091826347f3ba5af986a4bbf9e0038b3154962f49454739?d=identicon)[gemul](/maintainers/gemul)

---

Top Contributors

[![gemul](https://avatars.githubusercontent.com/u/4983413?v=4)](https://github.com/gemul "gemul (7 commits)")

---

Tags

jsonjson-parser

### Embed Badge

![Health badge](/badges/gemul-jsonparser/health.svg)

```
[![Health](https://phpackages.com/badges/gemul-jsonparser/health.svg)](https://phpackages.com/packages/gemul-jsonparser)
```

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k334.7M790](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k504.8M167](/packages/mtdowling-jmespathphp)[jms/serializer

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

2.3k141.9M929](/packages/jms-serializer)[jms/serializer-bundle

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

1.8k92.4M679](/packages/jms-serializer-bundle)[cerbero/json-parser

Zero-dependencies pull parser to read large JSON from any source in a memory-efficient way.

806588.3k5](/packages/cerbero-json-parser)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30525.1M56](/packages/colinodell-json5)

PHPackages © 2026

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