Download the PHP package gemul/jsonparser without Composer
On this page you can find all versions of the php package gemul/jsonparser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gemul/jsonparser
More information about gemul/jsonparser
Files in gemul/jsonparser
Package jsonparser
Short Description A PHP Library to easily handle JSON data
License Apache-2.0
Informations about the package jsonparser
PHP JSON Parser
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
-
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
To get started, you can install the library using Composer:
Usage
1. Load JSON Data
Reading JSON from String
or from JSON file
you can alson define separator string used for traversing the json tree, in constructor's second parameter (default ".").
2. Getting JSON data
Given this json structure:
you can use to traverse the json tree and get the data.
it can also return the rest of the json branches, for example
Handling path that doesn't 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.
3. Set a JSON data
In order to set a data to the tree, you can use for example
the json would become
You can safely make new depth to the path
Setting Array
For array data, you can explicitly use index, or use '[]' to change or append an element into either existing or new array.
will result in
3. Get last valid path
After executing , you can use to retrieve the last valid path that was successfully traversed by the getItem method.
4. Get the current full JSON object
To get the current json as an object, use . Or alternatively the json-encoded string using .
5. Save json string to file
To save the json string, use
make sure that the directory is writable.