I'm working with json files and I need to include a json file into an other one.
These are my code and expected reuslt :
file1.json
{
"object" : {
"name" : "society",
"type" : "string",
"database" : "society"
},
"global" : {
// HERE IS THE file2.json CONTENT
}
}
file2.json
{
"email" : {
"type" : "string",
"color" : "secondary",
"logo" : "email.jpg"
},
"quizz" : {
"type" : "string",
"color" : "primary",
"logo" : "quizz.jpg"
}
}
Moreover, I already try this :
"..." : "json file2.json"
But it didn't work. I also use php script to use my json file so maybe I can include file2.json at this time. I also really need to separate my json because I want to use file2.json in many different json.
Plus, I am not creating my json with Php (if it wasn't clear), I just use them later.
Thank,
Lucas.
Ok, so I found a solution. I use php to update my json when I want to use it.
I've put a key in my json file which allows me to include the json file.
Here is the json code
{
//YOUR JSON CODE AND WHEN YOU WANT TO INCLUDE ANOTHER JSON FILE DO :
"..." : "PATH_TO_JSON"
}
Here is the PHP code :
function json_include(&$array){
foreach($array as $key => &$value){
if(is_array($value)){
json_include($value);
}
else{
if($key = "..."){
if_file_exists("YOUR_PATH_TO_JSON"){
$value=json_decode(file_get_contents("YOUR_PATH_TO_JSON".$value), true);
$array+=$value;
unset($array[$key]);
}
if(is_array($value)){
json_included($value);
}
}
}
}
Plus, if you want to update you json file you just ask to php to write into your json.file.
If you do so, you'll can't update your new json (because the key is deleted)
Here is PHP code to update file1.json
//Let's say $my_arr is your file1.json (encoded as a php array)
json_include($my_arr);
file_put_contents(file1.json, json_encode($my_arr));
I didn't try this code because my usage is a little bit different then is there is correction please let me know.
Have fun :)