0

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.

Lucas
  • 73
  • 1
  • 8
  • Possible duplicate of [create nested JSON object in php?](https://stackoverflow.com/questions/15810257/create-nested-json-object-in-php) – Andrei Lupuleasa Aug 01 '19 at 07:16
  • Get both files content by `file_get_content()` and `merge` both of json and put into new file – PHP Ninja Aug 01 '19 at 07:16
  • @AndreiLupuleasa I don't really thing that is the same thing. I don't create json with php but I decode my json files in php – Lucas Aug 01 '19 at 07:19
  • @Gulshan so I should put an element in file1.json to know where including the other one ? – Lucas Aug 01 '19 at 07:23
  • you can not include json file as you want but you can append json file 2 data to the json file 1 or json file 1 data to json file 2 – Priyank Aug 01 '19 at 07:24
  • @Priyank my question will be how ? :) – Lucas Aug 01 '19 at 07:29
  • 1
    @Lucas check this answer ( https://stackoverflow.com/questions/7895335/append-data-to-a-json-file-with-php ) Let me know still you didn't get it – Priyank Aug 01 '19 at 07:30
  • @Priyank I got the idea, I'm gonna create a php function to include file2.json where I want, thank – Lucas Aug 01 '19 at 07:42
  • welcome @Lucas , let me know if you have any query. – Priyank Aug 01 '19 at 07:44

2 Answers2

1

json is a container of information, a machine reading it needs to still understand what the data is. You cannot infer that any data you put into a json file will be understood by a third party. As such "referencing" another json file would have to be something you design into your reader to understand then when it finds this pointer, that it would go off and download that file and continue to parse it.

Pulse-Eight
  • 101
  • 3
0

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 :)

Lucas
  • 73
  • 1
  • 8