Skip to content
Menu
Tech Automation Blog
  • About Author
  • Contact
Tech Automation Blog

Merge multiple JSON files to one file

Posted on August 30, 2019February 27, 2022

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
}

Source JSON files
Error when supplied wrong path
Script execution
Merged JSON file after execution

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« May    

Recent Posts

  • Monitor and alert Azure Service Health issues May 5, 2020
  • AWS IAM User access review May 3, 2020
  • Integrate Azure Security Center with Event Hub April 28, 2020
  • Add Tags to Azure Subscription April 24, 2020
  • Automate Azure billing report in Excel March 6, 2020

Categories

©2025 Tech Automation Blog | Powered by SuperbThemes