bitwerft / json-include
Provides functionality to include content of JSON files into another JSON files.
1.0.0
2025-02-24 10:33 UTC
Requires
- php: ^8.1
This package is not auto-updated.
Last update: 2025-07-17 15:49:45 UTC
README
This allows you to use #include
within .json
files, so you can break up large json files into multiple smaller ones.
Installation
Through composer:
- Simply execute
composer require bitwerft/json-include
Usage
1. JSON File
- In your json file add a "#include" as the key, with the name (if in the same folder) or the path to the file as the value
- This can be in the middle of an array, as the only array element or nested deep in the file. The content of the included json will be placed at the exact same place.
- This file needs to be a valid json
- There is a maximum Number of includes, which is by default set to 100 to prevent circular includes
use Bitwerft\JsonInclude\JsonInclude;
- Execute using
JsonInclude::render('filename.json');
2. Included JSON Files
- The included files need to be valid json themselves
- They don't need to have the
.json
file extension - Every file can be included multiple times
- They can also have "#include"s, which will be included recursivly
- Circular includes will cause an error
3. Result
- The assembled json will be returned as an unformatted string
- By default if any include fails, an Exception containing all info will be thrown
- The last used info array can also be accesed through
JsonInlude::$info
Example
- JSON File
{
"text": [
{"#include":"http://example.com/file.json"}
],
"#include":"file:///storage/app/file2.json"
}
- JSON Include 1 (http://example.com/file.json)
{
"paragraph1": {
"sentence": "Lorem ipsum"
},
"paragraph2": {
"sentence": "dolor sit amet"
}
}
- JSON Include 2 (file:///storage/app/file2.json)
{
"ids": [1, 2, 3, 4, 5]
}
- Result (with added formatting)
{
"text":[
{
"paragraph1": {
"sentence":"Lorem ipsum"
},
"paragraph2": {
"sentence":"dolor sit amet"
}
}
],
"ids": [1,2,3,4,5]
}