Recently I was processing some JSON files for one of my project and wanted to merge multiple similar JSON files in to a single file. All those files I was processing were in similar JSON format, however, coming from different sources. Once I have all these files stored in disk, I wanted to ingest in to my Firebase database in cloud.
A manual process used to take a lot of time and effort, thought, a small Powershell can help me to automate the file processing task quickly.
Here is my small script snippet to merge multiple JSON files in to one. It assumes all my small JSON files in the same directory. You can modify script to get those source files from different sources. In addition, set the “depth” parameter in ConvertTo-JSON based on your file structure. Default value is 2. I prefer to set the “Compress” parameter to minify JSON output.
Param(
[Parameter(Mandatory=$true)]
[String]$sourceDir
)
if(!(Test-Path $sourceDir)){
Write-Host "ERROR: Path not found.Check the path and try again!" -ForegroundColor Red
}
else{
$allFiles = @()
ForEach($i in Get-ChildItem $sourceDir){
$fileName = $i.Name
$data = Get-Content -Path $sourceDir\$fileName -Raw | ConvertFrom-Json
$allFiles += $data
}
$allFiles | ConvertTo-Json -Depth 2 -compress | Out-File -FilePath $sourceDir\merged.json
}