Jeisson Posted February 25, 2016 Share Posted February 25, 2016 Hello I have several json files in one directory. i would like to merge them in to one. I did something like this <?php foreach (glob("../json/*.json") as $filename) { echo basename($filename) . "\n"; } $res1 = file_get_contents($filename); echo $res1; ?> But it echoes only the last (the first echo is to view all files which is not important here) Then I tried many versions of foreach inside foreach with no success. Like this: <?php foreach (glob("../json/*.json") as $filename) { foreach (file_get_contents($filename) as $a) { } } echo $a; ?> So I appreciate a little help here, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/300886-merge-json-from-directory-to-one-file/ Share on other sites More sharing options...
Jacques1 Posted February 25, 2016 Share Posted February 25, 2016 (edited) If you want to do something with each $filename, you have to do it inside the loop. Doing it after the loop only gets you the last filename as a leftover. <?php $complete_json = ''; foreach (glob(__DIR__.'/../json/*.json') as $json_path) { $complete_json .= file_get_contents($json_path); } echo $complete_json; Note that a JSON document has a specific structure (it must be either an array or an object), so you probably can't just concatenate all individual files. You'll need to wrap them in a common structure. Edited February 25, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/300886-merge-json-from-directory-to-one-file/#findComment-1531459 Share on other sites More sharing options...
Jeisson Posted February 25, 2016 Author Share Posted February 25, 2016 thank you I will continue from here. The json files have all same structure so I thought I could merge them to one. But if that will not work I will try to get a specific key from each files directly. Maybe something like that would be better anyway. Quote Link to comment https://forums.phpfreaks.com/topic/300886-merge-json-from-directory-to-one-file/#findComment-1531463 Share on other sites More sharing options...
Jacques1 Posted February 25, 2016 Share Posted February 25, 2016 You can merge JSON documents, but you can't just concatenate them. If all documents have an object as their toplevel structure, you need to put the key/value pairs into a common object, possibly resolving key conflicts. If all documents are arrays, you can concatenate the individual elements. In both cases, array_merge() should be useful. But if you already have a better approach, that's of course preferrable. Quote Link to comment https://forums.phpfreaks.com/topic/300886-merge-json-from-directory-to-one-file/#findComment-1531465 Share on other sites More sharing options...
Jeisson Posted February 25, 2016 Author Share Posted February 25, 2016 I don't yet have any better approach I'm just thinking out loud. I will need to try and test. this was first step. Are you suggesting it would be safe way to decode everything then array_merge and encode this to json? Quote Link to comment https://forums.phpfreaks.com/topic/300886-merge-json-from-directory-to-one-file/#findComment-1531467 Share on other sites More sharing options...
Jacques1 Posted February 25, 2016 Share Posted February 25, 2016 If you're dealing with JSON arrays, yes. In case of objects, you need to be careful with conflicting keys. It might make more sense to keep each object in a separate “namespace”. For example, { "x": 1, "y": 3 } and { "x": 2 } would become { "a-meaningful-name-for-the-first-dataset" : { "x": 1, "y": 3 }, "a-meaningful-name-for-the-second-dataset" : { "x": 2 } } But I don't know your data, so this is a decision you'll have to make. Quote Link to comment https://forums.phpfreaks.com/topic/300886-merge-json-from-directory-to-one-file/#findComment-1531469 Share on other sites More sharing options...
Jeisson Posted February 25, 2016 Author Share Posted February 25, 2016 yeah I will see. My json files are not ready yet. So I will work on them when I have the code that does something with them. Iḿ just such a noob. May I ask why first code works json_decode($complete_json,true); var_dump($complete_json); while this not. $content = json_decode($complete_json,true); var_dump($content); I just don see the logic always Quote Link to comment https://forums.phpfreaks.com/topic/300886-merge-json-from-directory-to-one-file/#findComment-1531471 Share on other sites More sharing options...
Jacques1 Posted February 25, 2016 Share Posted February 25, 2016 The first code fragment doesn't do anything (useful). It decodes $complete_json, throws away the result and then dumps the original JSON-encoded data. The second fragment attempts to actually display the decoded content. Are you sure $complete_json is valid JSON? Like I already said, concatening individual JSON documents doesn't work, it was just a demonstration of loop logic. Quote Link to comment https://forums.phpfreaks.com/topic/300886-merge-json-from-directory-to-one-file/#findComment-1531472 Share on other sites More sharing options...
Jeisson Posted February 25, 2016 Author Share Posted February 25, 2016 Ok now I realize. I thought in my case they would be valid but that is not the case.Need to re-think the whole thing now. Something like decoding each file and then getting what I am lookin for and store that as new json. Whole merging and concatening I think I do not need.. Quote Link to comment https://forums.phpfreaks.com/topic/300886-merge-json-from-directory-to-one-file/#findComment-1531473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.