In my ASP.NET MVC Web project, I have got a number of JSON files lying around.
File1.json
{
"manage_employees_section_title":
{
"value": "Manage employees",
"description": "The mange employees section title"
}
}
File2.json
{
"manage_operations_section_title":
{
"value": "Manage operations
"description": "The mange operations section title"
}
}
I need to as part of my build process get all my JSONs
and merge into one single file.
I have used MSBuild like this.
<Target Name="ConcatenateJsFiles">
<ItemGroup>
<ConcatFiles Include="Application\**\resource-content*.json"/>
</ItemGroup>
<ReadLinesFromFile File="%(ConcatFiles.Identity)">
<Output TaskParameter="Lines" ItemName="ConcatLines"/>
</ReadLinesFromFile>
<WriteLinesToFile File="Infrastructure\Content\Store\ContentStore.json" Lines="@(ConcatLines)" Overwrite="true" />
</Target>
And this is what I got...
Concat.json
//What I got
{
"manage_employees_section_title":
{
"value": "Manage employees",
"description": "The mange employees section title"
}
}
{
"manage_operations_section_title":
{
"value": "Manage operations
"description": "The mange operations section title"
}
}
Even though I have achieved my goal of concatenation, what I really want is to merge all JSON
files into one JSON
object.
//What I really want
{
"manage_employees_section_title":
{
"value": "Manage employees",
"description": "The mange employees section title"
},
"manage_operations_section_title":
{
"value": "Manage operations
"description": "The mange operations section title"
}
}
How can I achieve this as part of my build process with Visual Studio.
Many thanks in advance guys..
It was interesting task to obtain the result using MSBuild functionality only... To be honest, creating additional C# application for this goal is better approach. But I was able to do it with MSBuild as well:
<Project ToolsVersion="12.0" DefaultTargets="ConcatenateJsFiles" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<Target Name="ConcatenateJsFiles">
<ItemGroup>
<ConcatFiles Include="Application\**\resource-content*.json"/>
</ItemGroup>
<ItemGroup>
<!-- Read file content (with spaces preserving), remove trailing { and } -->
<ContentLines Include="$([System.IO.File]::ReadAllText('%(ConcatFiles.Identity)').Remove($([MSBuild]::Subtract($([System.IO.File]::ReadAllText('%(ConcatFiles.Identity)').Length), 1)), 1).Remove(0, 1))"/>
<!-- Create resulting file with trailing { and } -->
<FileContent Include="{"/>
<FileContent Include="@(ContentLines, ',%0a')"/>
<FileContent Include="}"/>
</ItemGroup>
<WriteLinesToFile File="ContentStore.json" Lines="@(FileContent)" Overwrite="true" />
</Target>
</Project>
As a result you'll have the following file:
{
"manage_employees_section_title":
{
"value": "Manage employees",
"description": "The mange employees section title"
},
"manage_operations_section_title":
{
"value": "Manage operations",
"description": "The mange operations section title"
}
}
This approach is not flexible enough, for example it requires trailing braces to be located at the first and at the last positions of source files. Anyway, it is enough to demonstrate how to do it with MBSuild only.