.NET Standard library allows to transform source Newtonsoft.Json.Linq.JToken object recursively using extendable "Transformers"
Note: Current implementation actually MUTATES source node provided for transformation
//entryFile.json:
{
"linkedFile": { "$file": "shared.json" }
}
///shared.json:
{
"myFile2Prop1": 123
}
// new JsonTransformationService(new LoadFileTransform()).Transform(entryFileJToken):
{
"linkedFile": {
"myFile2Prop1": 123
}
}
//file.json:
{
"templates": {
"t1": {
"tp1": 123,
"tp2": "Hello world"
}
},
"jobs": {
"job1": {
"job1Prop1": 1235,
"$merge": "$.templates.t1"
}
}
}
//new JsonTransformationService(new MergeTransform()).Transform(fileJToken.SelectToken("$.jobs.job1"));
{
"job1Prop1": 1235,
"tp1": 123,
"tp2": "Hello world"
}
// file.json:
{
"templates": {
"t1": {
"name": { "$useParameter": "jobName" },
"tp1": 123,
"tp2": "Hello world"
}
},
"jobs": {
"job1": {
"job1Prop1": 1235,
"$merge": "$.templates.t1",
"$setParameters": {
"jobName": "My Job 1"
}
}
}
}
//new JsonTransformationService( new MergeTransform(), new ParametersTransform())
// .Transform(fileJToken.SelectToken("$.jobs.job1")):
{
"job1Prop1": 1235,
"name": "My Job 1",
"tp1": 123,
"tp2": "Hello world"
}
If you need additional transformers, just implement interface ICanTransformJson and add it in ctor parameters of your jsonTransformationService instance.
Use nuget package: https://www.nuget.org/packages/JsonTransformation/