Report inadequate content

Merge Json files

{
}

With this small script you could merge some json files in a new one in json validated format.  The script uses each file name like array key.

Look the example:

file1.json:

{"array1":["elem1","elem2","elem3"],"array2":["elem1","elem2","elem3"]}

file2.json

{"array3":["elem1","elem2","elem3"]}

 

Whit our script you could run:

  • php merge_jsons.php file1.json file2.json > merged_jsons.json

 

Now you can open merged_jsons.json and look this:

{"file1":["array1":["elem1","elem2","elem3"],"array2":["elem1","elem2","elem3"]],
"file2":["array3":["elem1","elem2","elem3"]]}

The script:

for ( $i=1; $i{
$file_name = basename($argv[$i]);
$key_name = strstr($file_name,'.',true);
$data = file_get_contents( $argv[$i] );
$result[$key_name]=json_decode( $data );}echo json_encode( $result );
}
{
}
{
}

Leave your comment Merge Json files

Log in to Obolog, or create your free blog if you are not registered yet.

User avatar Your name