iamjohndev/json-reform

A simple PHP Library that can read and format JSON data.

dev-main 2023-12-09 04:12 UTC

This package is auto-updated.

Last update: 2024-09-09 05:56:31 UTC


README

A simple PHP library for reading, formatting and getting values of a JSON data.

Installation

Install via Composer: composer require iamjohndev/json-reform:dev-main

Usage

    use iamjohndev\JSONReform;
    
    // Read JSON data from a string
    $json = '{"name": "John", "age": 30}';
    $reader = new JSONReform($json);
    
    // Get a value from the JSON data
    $name = $reader->getValue('name'); // "John"
    
    // Format the JSON data
    $prettyJson = $reader->format('pretty'); // "{\n    "name": "John",\n    "age": 30\n}"
    
    // Read JSON data from a file
    $reader = JSONReform::fromFile('data.json');
    
    // Get a nested value from the JSON data
    $value = $reader->getValue('data.persons.0.name'); // "John"

API

JSONReform::__construct(string $json)
// Creates a new JSONReform instance from a JSON string.

JSONReform::fromFile(string $path)
// Creates a new JSONReform instance from a JSON file.

JSONReform::getValue(string $path, mixed $default = null): mixed
// Gets the value at the specified path in the JSON data.

JsonReform::fromHTTPRequestBody();
// Creates a new JsonReform instance from an HTTP Request Body

JSONReform::format(string $format = 'json'): string
// Returns the JSON string in the specified format.