Jump to content
Jeisson

merge json from directory to one file

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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 by Jacques1

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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..

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.