Codescovery.JsonServices 1.0.2

.NET Core 3.1
dotnet add package Codescovery.JsonServices --version 1.0.2
NuGet\Install-Package Codescovery.JsonServices -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Codescovery.JsonServices" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Codescovery.JsonServices --version 1.0.2
#r "nuget: Codescovery.JsonServices, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Codescovery.JsonServices as a Cake Addin
#addin nuget:?package=Codescovery.JsonServices&version=1.0.2

// Install Codescovery.JsonServices as a Cake Tool
#tool nuget:?package=Codescovery.JsonServices&version=1.0.2

Codescovery JsonServices

Useful library to convert a json object to json paths. Also provides the ability to override the value from a json path.

Pre requisites

Your project must be running under .Net Core 3.1 or higher.

How to use

Dependency Injection

   public void ConfigureServices(IServiceCollection services)
        {
           ...
            services.AddJsonServices();
           ...
        }

Factory

var jsonService = JsonServiceFactory.Create();

Result

Take as example the following json:

  {
     "person":{
        "name":"FirstName",
        "age":30,
        "childs":[
           {
              "name":"child1",
              "age":16
           },
           {
              "name":"child2",
              "age":18
           }
        ],
        "likedFruits":[
           "apple",
           "banana"
        ]
     }
  }

The following code:

  var service = JsonServiceFactory.Create();
  var jsonPath = service.ConvertToJsonPath(json);
  var paths = jsonPath.GetJsonPaths();

Will return an IDictiorary<string,string> like this:

{
  "person":"{'name':'FirstName','age':30,'childs':[{'name':'child1','age':16},{'name':'child2','age':18}],'likedFruits':['apple','banana']}",
  "person.name":"FirstName",
  "person.age":"30",
  "person.childs":"[{'name':'child1','age':16},{'name':'child2','age':18}]",
  "person.childs[0].name":"child1",
  "person.childs[0].age":"16",
  "person.childs[1].name":"child2",
  "person.childs[1].age":"18",
  "person.likedfruits":"['apple','banana']",
  "person.likedfruits[0]":"apple",
  "person.likedfruits[1]":"banana"
}

Overriding Values

This library has the ability to override json values based on paths. Take this 2 jsons as examples.

JSON1

  {
     "person":{
        "name":"FirstName",
        "age":30,
        "childs":[
           {
              "name":"child1",
              "age":16
           },
           {
              "name":"child2",
              "age":18
           }
        ],
        "likedFruits":[
           "apple",
           "banana"
        ]
     }
  }

JSON2

{
   "person":{
      "name":"FirstName and LastName",
      "age":34,
      "likedFruits":[
         "pineapple",
         "avocado",
         "strawberry"
      ]
   }
}

If you use the following code:

  var service = JsonServiceFactory.Create();
  var result = service.OverrideJsonValuesFromPath(json2, json1, writeIdented: true);

It will return:

  {
     "person":{
        "name":"FirstName and LastName",
        "age":34,
        "childs":[
           {
              "name":"child1",
              "age":16
           },
           {
              "name":"child2",
              "age":18
           }
        ],
        "likedFruits":[
           "pineapple",
           "avocado",
           "strawberry"
        ]
     }
  }
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp3.1
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 299 6/23/2021