iteks / laravel-json
A Laravel package for simplified JSON data manipulation, offering seamless conversion to collections or arrays with attribute filtering.
Installs: 2 198
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- laravel/framework: ^9.46|^10.10|^11.0
Requires (Dev)
- laravel/pint: ^1.14
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- phpstan/phpstan: ^1.10
- symfony/var-dumper: ^6.4
This package is auto-updated.
Last update: 2024-05-06 01:34:48 UTC
README
The Laravel JSON package is a powerful and versatile tool designed to enhance the handling of JSON data within Laravel applications. With its intuitive API, developers can effortlessly convert JSON files into Laravel collections or associative arrays, facilitating easy data manipulation and access. Whether you're dealing with configuration files, dataset imports, or any JSON-formatted data source, this package simplifies the process, allowing you to focus on building feature-rich applications. Built with flexibility in mind, it supports optional attribute filtering, enabling precise data retrieval tailored to your needs. Perfect for projects of all sizes, Laravel JSON aims to streamline your development workflow, making JSON data handling a breeze. Offered by iteks, Developed by jeramyhing.
Get Started
Requires PHP 8.1+
Install Laravel JSON via the Composer package manager:
composer require iteks/laravel-json
Usage
Sample Json Dataset
[ { "border": "3px solid white", "coordinates": [ 51.70696, 40.34103 ], "password": "xXeagle-*******" }, { "border": "1px dotted amber", "coordinates": [ 11.80583, 108.05094 ], "password": "xXmeerkat-******" }, { "border": "4px dotted gray", "coordinates": [ 116.82882, 74.57905 ], "password": "xXcat-*******" } ]
JSON Helpers
First, import the helper class:
use Iteks\Support\Facades\Json;
You may then use the following methods:
Json::toCollection()
The toCollection
method converts a JSON file into a Laravel collection of collections. This is particularly useful when you need to manipulate JSON data with the convenience and power of Laravel's Collection methods.
Without Argument Usage
When you use toCollection without specifying an attribute, it will simply convert the entire JSON file into a collection where each element is itself a collection representing the JSON objects.
$collection = Json::toCollection(database_path('data/test.json'));
Illuminate\Support\Collection {#298 ▼ #items: array:3 [▼ 0 => Illuminate\Support\Collection {#299 ▼ #items: array:3 [▼ "border" => "3px solid white" "coordinates" => array:2 [▼ 0 => 51.70696 1 => 40.34103 ] "password" => "xXeagle-*******" ] #escapeWhenCastingToString: false } 1 => Illuminate\Support\Collection {#300 ▼ #items: array:3 [▼ "border" => "1px dotted amber" "coordinates" => array:2 [▼ 0 => 11.80583 1 => 108.05094 ] "password" => "xXmeerkat-******" ] #escapeWhenCastingToString: false } 2 => Illuminate\Support\Collection {#301 ▼ #items: array:3 [▼ "border" => "4px dotted gray" "coordinates" => array:2 [▼ 0 => 116.82882 1 => 74.57905 ] "password" => "xXcat-*******" ] #escapeWhenCastingToString: false } ] #escapeWhenCastingToString: false }
// Iterate over the collection foreach ($collection as $item) { // Access properties like in any Laravel collection echo $item->get('border'); }
With Argument Usage
When you provide an attribute name as the second argument, toCollection will create a collection where each item is the value of the specified attribute from the JSON objects.
$collection = Json::toCollection(database_path('data/test.json'), 'border');
Illuminate\Support\Collection {#297 ▼ #items: array:3 [▼ 0 => "3px solid white" 1 => "1px dotted amber" 2 => "4px dotted gray" ] #escapeWhenCastingToString: false }
Json::toArray()
The toArray
method converts a JSON file into a PHP array. This method is ideal for when you need a simple array representation of your JSON data for further processing or when Laravel's Collection methods are not necessary.
Without Argument Usage
Without specifying an attribute, toArray converts the entire JSON file into a nested array, with each element being an associative array representing the JSON objects.
$array = Json::toArray(database_path('data/test.json'));
array:3 [▼ 0 => array:3 [▼ "border" => "3px solid white" "coordinates" => array:2 [▼ 0 => 51.70696 1 => 40.34103 ] "password" => "xXeagle-*******" ] 1 => array:3 [▼ "border" => "1px dotted amber" "coordinates" => array:2 [▼ 0 => 11.80583 1 => 108.05094 ] "password" => "xXmeerkat-******" ] 2 => array:3 [▼ "border" => "4px dotted gray" "coordinates" => array:2 [▼ 0 => 116.82882 1 => 74.57905 ] "password" => "xXcat-*******" ] ]
// Access the array directly foreach ($array as $item) { echo $item['border']; }
With Argument Usage
Providing an attribute name as the second argument, toArray will generate an array containing only the values of the specified attribute from each JSON object.
$array = Json::toArray(database_path('data/test.json'), 'border');
array:3 [▼ 0 => "3px solid white" 1 => "1px dotted amber" 2 => "4px dotted gray" ]