Let's say you have a directory full of JSON files and you need to merge them into a single file.
Emp1.json
"Name": "Mike",
"ID": 123,
"Status": "Active"
}
Emp2.json
"Name": "Susan",
"ID": 124,
"Status": "Active"
}
Emp3.json
"Name": "Sam",
"ID": 125,
"Status": "Inactive"
}
You need a result that includes commas and square brackets that looks like this:
[
{
"Name": "Mike",
"ID": 123,
"Status": "Active"
},
{
"Name": "Susan",
"ID": 124,
"Status": "Active"
},
{
"Name": "Sam",
"ID": 125,
"Status": "Inactive"
}
]
{
"Name": "Mike",
"ID": 123,
"Status": "Active"
},
{
"Name": "Susan",
"ID": 124,
"Status": "Active"
},
{
"Name": "Sam",
"ID": 125,
"Status": "Inactive"
}
]
Solution 1:
Combing the files using Get-Content and append the start and end brackets.
"[" + ((Get-Content Emp*.json -raw) -join "," ) + "]" | Out-File AllEmp.json
Note that the "-raw" reads the entire file as a single string, not an array of strings. That's how we can add a comma between each block of JSON. So, each file is read as a single string, each string is added by Get-Content to an array. The array of JSON strings is then joined into a single long string with a comma between each JSON string.
You might want the brackets on their own lines, so add some line breaks.
"[`n" + ((Get-Content Emp*.json -raw) -join "," ) + "`n]" | Out-File AllEmp.json
What if you wanted to sort them or manipulate the objects' properties as they are merged? You will need to convert them to objects first, then sort or manipulate the objects, and then convert them back to JSON.
( "[" + ((Get-Content Emp*.json -raw) -join "," ) + "]" | ConvertFrom-Json ) | sort name | ConvertTo-Json | Out-File AllEmp.json
What if you have a lot of files? The examples above bring all of the files into memory and create one large string. Here's a solution that reads each file and appends to the merged file.
- The first line creates the new file and writes out the starting bracket.
- The second line creates our delimiter and sets it "" for the first item.
- The third line gets each JSON file and writes it to the new file with a leading delimiter. After the first file it sets the delimiter to ",".
- The last line adds the closing square bracket.
"[" | Out-File AllEmps.json -Force
$delim = "";
Get-ChildItem Emp*.json | foreach { $delim + (Get-Content $_ -raw); $delim="," } | Out-File AllEmps.json -Append }
"]" | Out-File AllEmps.json -Append
No comments:
Post a Comment
Note to spammers...
Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.