Click here to Skip to main content
13,797,212 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

51K views
48 bookmarked
Posted 14 May 2017
Licenced CPOL

JUST - JSON Under Simple Transformation

, 11 Jun 2018
Rate this:
Please Sign up or sign in to vote.
JSON to JSON transformation - XSLT equivalent for JSON

 

Introduction

The article describes how to use the JUST.NET library to transform JSON documents.

Background

JUST stands for JSON Under Simple Transformation.XSLT is a very popular way of transforming XML documents using a simple transformation language.

More and more applications are now using JSON as a data format because it is much simpler and less bulkier than XML.

However, there isn't a very easy way to transforming JSON documents.

I have created a library in .NET which enables the transformation of JSON documents using very a simple transformation language. This is an attempt to create an XSLT parallel for JSON.

This article describes how to use that library.

Install the Nuget Package

Pull the latest JUST.NET from https://www.nuget.org.

Install-Package JUST

 

A dotnetcore version is also available:

Install-Package JUST.NETCore

 

A .NET standard version is also available. This is the versionn which will be supported from now on.

Install-Package JUST.NET

Using the Code

Once you download the Nuget package, you create a simple console application.
Below is a simple C# code snippet that you can use to transform your JSON:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using JUST;
using System.IO;

namespace JUST.Test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string input = File.ReadAllText("Input.json");

            string transformer = File.ReadAllText("Transformer.json"); ;

            string transformedString = JsonTransformer.Transform(transformer, input);

            Console.WriteLine(transformedString);           
        }
    }
}

Here Input.json is the input JSON document while Transformer.json is the JSON document that transforms the input JSON.

Using JUST to Transform JSON

JUST is a transformation language just like XSLT. It includes functions which are used inside the transformer JSON to transform the input JSON in a desired output JSON. This section describes the various functions present in JUST and how they can be used to transform your JSON.

Every JUST function starts with "#" character.

valueof

This function is used to extract the value of a given property. The value is extracted using JSON path of the property. For more information on how to use JSON path, refer to:

Consider the input:

{
  "menu": {  
    "popup": {
      "menuitem": [
       {
          "value": "Open",
          "onclick": "OpenDoc()"
        },
        {
          "value": "Close",
          "onclick": "CloseDoc()"
        }
      ]
    }
  }
}

Transformer:

{
  "result": {
    "Open": "#valueof($.menu.popup.menuitem[?(@.value=='Open')].onclick)",
    "Close": "#valueof($.menu.popup.menuitem[?(@.value=='Close')].onclick)"
  }
}

Output:

{
   "result":{"Open":"OpenDoc()","Close":"CloseDoc()"}
}

ifcondition

This condition is used to evaluate an if-else condition.

ifcondition(condition expresson, evaluation expression, true result, false result)

All four of the parameters can be a 'valueof' expressions or constants.

Consider the input:

{
  "menu": {
    "id" : "github",
    "repository" : "JUST"
  }
}

Transformer:

{
  "ifconditiontesttrue": "#ifcondition
  (#valueof($.menu.id),github,#valueof($.menu.repository),fail)",
  "ifconditiontestfalse": "#ifcondition
  (#valueof($.menu.id),xml,#valueof($.menu.repository),fail)"
}

Output:

{
   "ifconditiontesttrue":"JUST",
   "ifconditiontestfalse":"fail"
}

string and math Functions

At the moment, only the basic and often used string and math functions are provided in the library.

  • lastindexof(input string,search string)
  • firstindexof(input string,search string)
  • substring(input string,start index,length)
  • concat(string 1,string 2)
  • add(value 1,value 2)
  • subtract(value 1,value 2)
  • multiply(value 1,value 2)
  • divide(value 1,values 2)

Consider the input:

{
  "stringref": "thisisandveryunuasualandlongstring",
  "numbers": [ "1", "2", "3", "4", "5" ]
}

Transformer:

{
  "stringresult": {
    "lastindexofand": "#lastindexof(#valueof($.stringref),and)",
    "firstindexofand": "#firstindexof(#valueof($.stringref),and)",
    "substring": "#substring(#valueof($.stringref),9,11)",
    "concat": "#concat(#valueof($.menu.id.file),#valueof($.menu.value.Window))"
  },
  "mathresult": {
    "add": "#add(#valueof($.numbers[0]),3)",
    "subtract": "#subtract(#valueof($.numbers[4]),#valueof($.numbers[0]))",
    "multiply": "#multiply(2,#valueof($.numbers[2]))",
    "divide": "#divide(9,3)"
  }
}

Output:

{"stringresult":
   {
    "lastindexofand":"21",
    "firstindexofand":"6",
    "substring":"veryunuasua",
    "concat":""
   },
   "mathresult":
   {
    "add":"4",
    "subtract":"4",
    "multiply":"6",
    "devide":"3"
   }
}

Opearators

The following operators have been added to compare strings and numbers :-
 
  • stringequals(string1, string2)
  • stringcontains(string1, string2)
  • mathequals(decimal1, decimal2)
  • mathgreaterthan(decimal1, decimal2)
  • mathlessthan(decimal1, decimal2)
  • mathgreaterthanorequalto(decimal1, decimal2)
  • mathlessthanorequalto(decimal1, decimal2)
Consider the input:- 
{
  "d": [ "one", "two", "three" ],
  "numbers": [ "1", "2", "3", "4", "5" ]
}
 
Transformer:-
{
  "mathresult": {
    "third_element_equals_3": "#ifcondition(#mathequals(#valueof($.numbers[2]),3),true,yes,no)",
    "third_element_greaterthan_2": "#ifcondition(#mathgreaterthan(#valueof($.numbers[2]),2),true,yes,no)",
    "third_element_lessthan_4": "#ifcondition(#mathlessthan(#valueof($.numbers[2]),4),true,yes,no)",
    "third_element_greaterthanorequals_4": "#ifcondition(#mathgreaterthanorequalto(#valueof($.numbers[2]),4),true,yes,no)",
    "third_element_lessthanoreuals_2": "#ifcondition(#mathlessthanorequalto(#valueof($.numbers[2]),2),true,yes,no)",
    "one_stringequals": "#ifcondition(#stringequals(#valueof($.d[0]),one),true,yes,no)",
    "one_stringcontains": "#ifcondition(#stringcontains(#valueof($.d[0]),n),true,yes,no)"
  }
}
Output:-
{"mathresult":   {"third_element_equals_3":"yes","third_element_greaterthan_2":"yes","third_element_lessthan_4":"yes","third_element_greaterthanorequals_4":"no","third_element_lessthanoreuals_2":"no","one_stringequals":"yes","one_stringcontains":"yes"}}

Aggregate Functions

The following aggregate functions are provided for single dimensional arrays:

  • concatall(array)
  • sum(array)
  • average(array)
  • min(array)
  • max(array)
Consider the input:-
{
  "d": [ "one", "two", "three" ],
  "numbers": [ "1", "2", "3", "4", "5" ]
}

Transformer:

{
  "conacted": "#concatall(#valueof($.d))",
  "sum": "#sum(#valueof($.numbers))",
  "avg": "#average(#valueof($.numbers))",
  "min": "#min(#valueof($.numbers))",
  "max": "#max(#valueof($.numbers))"
}

Output:

{
    "conacted":"onetwothree",
    "sum":"15",
    "avg":"3",
    "min":"1",
    "max":"5"
}

Aggregate Functions for Multidimensional Arrays

These functions are essentially the same as the above ones, the only difference being that you can also provide a path to point to particluar element inside the array:

  • concatallatpath(array,path)
  • sumatpath(array,path)
  • averageatpath(array,path)
  • minatpath(array,path)
  • maxatpath(array,path)

Consider the input:

{
   "x": [
    {
      "v": {
        "a": "a1,a2,a3",
        "b": "1",
        "c": "10"
      }
    },
    {
      "v": {
        "a": "b1,b2",
        "b": "2",
        "c": "20"
      }
    },
    {
      "v": {
        "a": "c1,c2,c3",
        "b": "3",
        "c": "30"
      }
    }
  ]
}

Transformer:

{
  "arrayconacted": "#concatallatpath(#valueof($.x),$.v.a)",
  "arraysum": "#sumatpath(#valueof($.x),$.v.c)",
  "arrayavg": "#averageatpath(#valueof($.x),$.v.c)",
  "arraymin": "#minatpath(#valueof($.x),$.v.b)",
  "arraymax": "#maxatpath(#valueof($.x),$.v.b)"
}

Output:

{
    "arrayconacted":"a1,a2,a3b1,b2c1,c2,c3",
    "arraysum":"60",
    "arrayavg":"20",
    "arraymin":"1",
    "arraymax":"3"
}

Bulk Functions

All the above functions set property values to predefined properties in the output JSON. However, in some cases, we don't know what our output will look like as it depends on the input. Bulk functions are provided for this purpose. They correspond with the template-match functions in XSLT.

Bulk functions by law have to be the first property of the JSON object. All bulk functions are represented as array elements of the property '#'.

These are the bulk functions provided as of now:

  • copy(path)
  • replace(path)
  • delete(path)

Consider the input:

{
  "tree": {
    "branch": {
      "leaf": "green",
      "flower": "red",
      "bird": "crow",
      "extra": { "twig":"birdnest" }
    },
    "ladder": {"wood": "treehouse" }
  }
}

Transformer:

{
  "#": [ "#copy($)",  "#delete($.tree.branch.bird)", 
         "#replace($.tree.branch.extra,#valueof($.tree.ladder))" ],
  "othervalue" : "othervalue"
}

Output:

{
   "othervalue":"othervalue",
   "tree":{
    "branch":{
     "leaf":"green",
     "flower":"red",
     "extra":{
      "wood":"treehouse"
     }
    },
    "ladder":{
     "wood":"treehouse"
    }
  }
}

Array Looping

In some cases, we don't want to copy the entire array to the destination JSON. We might want to transform the array into a different format, or have some special logic for each element while setting the destination JSON.
For these cases, we would use array looping.

These are the functions provided for this pupose:

  • loop(path) - path is the path of the array to loop
  • currentvalue()
  • currentvalueatpath(path) - here path denotes the path inside the array
  • lastvalueatpath(path) - here path denotes the path inside the array
  • currentindex()
  • lastindex()
  • lastvalue()

Cosider the input:

{
  "tree": {
    "branch": {
      "leaf": "green",
      "flower": "red",
      "bird": "crow",
      "extra": { "twig": "birdnest" }
    },
    "ladder": { "wood": "treehouse" }
  },
  "numbers": [ "1", "2", "3", "4" ],
  "arrayobjects": [
    {"country": {"name": "norway","language": "norsk"}},
    {
      "country": {
        "name": "UK",
        "language": "english"
      }
    },
    {
      "country": {
        "name": "Sweden",
        "language": "swedish"
      }
    }]
}

Transformer:

{
  "iteration": {
    "#loop($.numbers)": {
      "CurrentValue": "#currentvalue()",
      "CurrentIndex": "#currentindex()",
      "IsLast": "#ifcondition(#currentindex(),#lastindex(),yes,no)",
      "LastValue": "#lastvalue()"
    }
  },
  "iteration2": {
    "#loop($.arrayobjects)": {
      "CurrentValue": "#currentvalueatpath($.country.name)",
      "CurrentIndex": "#currentindex()",
      "IsLast": "#ifcondition(#currentindex(),#lastindex(),yes,no)",
      "LastValue": "#lastvalueatpath($.country.language)"
    }
  },
  "othervalue": "othervalue"
}

Output:

{

"iteration":[
   {"CurrentValue":"1","CurrentIndex":"0",
   "IsLast":"no","LastValue":"4"},
   {"CurrentValue":"2","CurrentIndex":"1",
   "IsLast":"no","LastValue":"4"},
   {"CurrentValue":"3","CurrentIndex":"2",
   "IsLast":"no","LastValue":"4"},
   {"CurrentValue":"4","CurrentIndex":"3",
   "IsLast":"yes","LastValue":"4"}
  ],
   "iteration2":[
   {"CurrentValue":"norway","CurrentIndex":"0",
   "IsLast":"no","LastValue":"swedish"},
   {"CurrentValue":"UK","CurrentIndex":"1",
   "IsLast":"no","LastValue":"swedish"},
   {"CurrentValue":"Sweden","CurrentIndex":"2",
   "IsLast":"yes","LastValue":"swedish"}
  ],
"othervalue":"othervalue"
}

 

Nested array looping (looping within context)


A new function loopwithincontext has been introduced to be able to loop withing the context of an outer loop.

Cosider the input:-
{
  "NestedLoop": {
    "Organization": {
      "Employee": [
        {
          "Name": "E2",
          "Details": [
            {
              "Country": "Iceland",
              "Age": "30",
              "Name": "Sven",
              "Language": "Icelandic"
            }
          ]
        },
        {
          "Name": "E1",
          "Details": [
            {
              "Country": "Denmark",
              "Age": "30",
              "Name": "Svein",
              "Language": "Danish"
            }
          ]
        }
      ]
    }
  }
}

Transformer:-
{
  "hello": {
    "#loop($.NestedLoop.Organization.Employee)": {
      "CurrentName": "#currentvalueatpath($.Name)",
      "Details": {
        "#loopwithincontext($.Details)": {
          "CurrentCountry": "#currentvalueatpath($.Country)"
        }
      }
    }
  }
}
 
Output:-
{ 
   "hello":[ 
      { 
         "CurrentName":"E2",
         "Details":[ 
            { 
               "CurrentCountry":"Iceland"
            }
         ]
      },
      { 
         "CurrentName":"E1",
         "Details":[ 
            { 
               "CurrentCountry":"Denmark"
            }
         ]
      }
   ]
}

 

Array grouping

 
A function similar to SQL GROUP BY clause has been introduced to group an array based on the value of an element.
 
grouparrayby(path,groupingElementName,groupedElementName)
 
Input:-
{
  "Forest": [
    {
      "type": "Mammal",
      "qty": 1,
      "name": "Hippo"
    },
    {
      "type": "Bird",
      "qty": 2,
      "name": "Sparrow"
    },
    {
      "type": "Amphibian",
      "qty": 300,
      "name": "Lizard"
    },
    {
      "type": "Bird",
      "qty": 3,
      "name": "Parrot"
    },
    {
      "type": "Mammal",
      "qty": 1,
      "name": "Elephant"
    },
    {
      "type": "Mammal",
      "qty": 10,
      "name": "Dog"
    }   
  ]
}
 
Transformer:-
{
  "Result": "#grouparrayby($.Forest,type,all)"
}
 
Output:-
{ 
   "Result":[ 
      { 
         "type":"Mammal",
         "all":[ 
            { 
               "qty":1,
               "name":"Hippo"
            },
            { 
               "qty":1,
               "name":"Elephant"
            },
            { 
               "qty":10,
               "name":"Dog"
            }
         ]
      },
      { 
         "type":"Bird",
         "all":[ 
            { 
               "qty":2,
               "name":"Sparrow"
            },
            { 
               "qty":3,
               "name":"Parrot"
            }
         ]
      },
      { 
         "type":"Amphibian",
         "all":[ 
            { 
               "qty":300,
               "name":"Lizard"
            }
         ]
      }
   ]
}
 
You can group using multiple "grouping elements". They should be seperated by a semicolon (:)
 
Consider the following input:-
 
{
  "Vehicle": [
    {
      "type": "air",
      "company": "Boeing",
      "name": "airplane"
    },
    {
      "type": "air",
      "company": "Concorde",
      "name": "airplane"
    },
    {
      "type": "air",
      "company": "Boeing",
      "name": "Chopper"
    },
    {
      "type": "land",
      "company": "GM",
      "name": "car"
    },
    {
      "type": "sea",
      "company": "Viking",
      "name": "ship"
    },
    {
      "type": "land",
      "company": "GM",
      "name": "truck"
    }
  ] 
}
 
Transformer:-
{
  "Result": "#grouparrayby($.Vehicle,type:company,all)"
}
 
Output:-
{ 
   "Result":[ 
      { 
         "type":"air",
         "company":"Boeing",
         "all":[ 
            { 
               "name":"airplane"
            },
            { 
               "name":"Chopper"
            }
         ]
      },
      { 
         "type":"air",
         "company":"Concorde",
         "all":[ 
            { 
               "name":"airplane"
            }
         ]
      },
      { 
         "type":"land",
         "company":"GM",
         "all":[ 
            { 
               "name":"car"
            },
            { 
               "name":"truck"
            }
         ]
      },
      { 
         "type":"sea",
         "company":"Viking",
         "all":[ 
            { 
               "name":"ship"
            }
         ]
      }
   ]
}
 
 

Calling Custom Functions

You can make your own custom functions in C# and call them from your transformer JSON.
A custom function has to reside inside a public class and has to be a public static method.

A custom function is called using the following syntax:

#customfunction(dll name, FQN for the static function, argument1.......,argumentN)

Consider the following input:

{
  "tree": {
    "branch": {
      "leaf": "green",
      "flower": "red",
      "bird": "crow"
    }
  }
}

Transformer:

{
  "Season": "#customfunction(JUST.NET.Test,JUST.NET.Test.Season.findseason,
             #valueof($.tree.branch.leaf),#valueof($.tree.branch.flower))"
}

Custom function:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JUST.NET.Test
{
    public class Season
    {
        public static string findseason(string leafColour, string flowerColour)
        {
            if (leafColour == "green" && flowerColour == "red")
                return "summer";
            else
                return "winter";
        }
    }
}

Output:

{"Season":"summer"}

Nesting of Functions

You can easily nest functions to do complex transformations.

An example of such a transformation is demonstrated by the example below:

Consider the following input:

{
  "Name": "Kari",
  "Surname": "Nordmann",
  "MiddleName": "Inger",
  "ContactInformation": "Karl johans gate:Oslo:88880000" ,
  "PersonalInformation": "45:Married:Norwegian"
}

Transformer:

{
  "FullName": "#concat(#concat
  (#concat(#valueof($.Name), ),#concat(#valueof($.MiddleName), )),#valueof($.Surname))",
  "Contact Information": {
    "Street Name": "#substring(#valueof($.ContactInformation),
    0,#firstindexof(#valueof($.ContactInformation),:))",
    "City": "#substring(#valueof($.ContactInformation),#add(#firstindexof
    (#valueof($.ContactInformation),:),1),#subtract(#subtract
    (#lastindexof(#valueof($.ContactInformation),:),#firstindexof
    (#valueof($.ContactInformation),:)),1))",
    "PhoneNumber": "#substring
    (#valueof($.ContactInformation),#add(#lastindexof
    (#valueof($.ContactInformation),:),1),#subtract
    (#lastindexof(#valueof($.ContactInformation),),#lastindexof
    (#valueof($.ContactInformation),:)))"
  },
  "Personal Information": {
    "Age": "#substring(#valueof($.PersonalInformation),
    0,#firstindexof(#valueof($.PersonalInformation),:))",
    "Civil Status": "#substring(#valueof
    ($.PersonalInformation),#add(#firstindexof
    (#valueof($.PersonalInformation),:),1),#subtract(#subtract
    (#lastindexof(#valueof($.PersonalInformation),:),#firstindexof
    (#valueof($.PersonalInformation),:)),1))",
    "Ethnicity": "#substring(#valueof
    ($.PersonalInformation),#add(#lastindexof
    (#valueof($.PersonalInformation),:),1),#subtract(#lastindexof
    (#valueof($.PersonalInformation),),#lastindexof
    (#valueof($.PersonalInformation),:)))"
  }

Output:

{
   "FullName":"Kari Inger Nordmann",
   "Contact Information":{
     "Street Name":"Karl johans gate",
     "City":"Oslo",
     "PhoneNumber":"88880000"
    },
   "Personal Information":{
     "Age":"45",
     "Civil Status":"Married",
     "Ethnicity":"Norwegian"
    }
}

Multiple Argument & Constant Functions

The transformation in the above scenario looks quite complex. And it could get quite messy when the string becomes longer. Also, since comma(,) is a reserved keyword, it is not possible to concatenate a comma to a string.

Hence, the following 3 functions have been introduced:

  • xconcat(string1,string2......stringx) - Concatenates multiple strings
  • xadd(int1,int2......intx) - Adds multiples integers
  • constant_comma() - Returns comma(,)
  • constant_hash() - Returns hash(#)

Consider the following input:

{
  "Name": "Kari",
  "Surname": "Nordmann",
  "MiddleName": "Inger",
  "ContactInformation": "Karl johans gate:Oslo:88880000" ,
  "PersonalInformation": "45:Married:Norwegian"
}

Transformer:

{
  "FullName": "#xconcat(#valueof($.Name),
  #constant_comma(),#valueof($.MiddleName),
               #constant_comma(),#valueof($.Surname))",
  "AgeOfParents": 
  "#xadd(#valueof($.AgeOfMother),#valueof($.AgeOfFather))"
}

Output:

{"FullName":"Kari,Inger,Nordmann",
"AgeOfParents":"67"}

Check for existence

The following two functions have been added to check for existence:-
 
  • exists(path)
  • existsandnotempty(path)
 
Consider the following input:-
{
   "BuyDate": "2017-04-10T11:36:39+03:00",
   "ExpireDate": ""
}
 
Transformer:-
{
  "BuyDateString": "#ifcondition(#exists($.BuyDate),true,#concat(Buy Date : ,#valueof($.BuyDate)),NotExists)",
  "BuyDateString2": "#ifcondition(#existsandnotempty($.BuyDate),true,#concat(Buy Date : ,#valueof($.BuyDate)),EmptyOrNotExists)",
  "ExpireDateString": "#ifcondition(#exists($.ExpireDate),true,#concat(Expire Date : ,#valueof($.ExpireDate)),NotExists)",
  "ExpireDateString2": "#ifcondition(#existsandnotempty($.ExpireDate),true,#concat(Expire Date : ,#valueof($.ExpireDate)),EmptyOrNotExists)",
  "SellDateString": "#ifcondition(#exists($.SellDate),true,#concat(Sell Date : ,#valueof($.SellDate)),NotExists)",
  "SellDateString2": "#ifcondition(#existsandnotempty($.SellDate),true,#concat(Sell Date : ,#valueof($.SellDate)),EmptyOrNotExists)"
}
 
Output:-
{

   "BuyDateString":"Buy Date : 2017-04-10T11:36:39+03:00",
   "BuyDateString2":"Buy Date : 2017-04-10T11:36:39+03:00",
   "ExpireDateString":"Expire Date : ",
   "ExpireDateString2":"EmptyOrNotExists",
   "SellDateString":"NotExists",
   "SellDateString2":"EmptyOrNotExists"
}
 
 

Conditional transformation

Conditional transformation can be achieved using the ifgroup function.
The function takes an expression as argument which should evaluate to a boolean value.
 
Consider the following input:-
{
  "Tree": {   
    "Branch": "leaf",
    "Flower": "Rose"
  }
}
 
Transformer:-
{
  "Result": {
    "Header": "JsonTransform",
    "#ifgroup(#exists($.Tree.Branch))": {
      "State": {
        "Value1": "#valueof($.Tree.Branch)",
        "Value2": "#valueof($.Tree.Flower)"
      }
    }
 }
}
 
Output:-
{ 
   "Result":{ 
      "Header":"JsonTransform",
      "State":{ 
         "Value1":"leaf",
         "Value2":"Rose"
      }
   }
}
 
Now, for the same input if we use the following transformer, we get a diferent output.
 
Transformer:-
{
  "Result": {
    "Header": "JsonTransform",
    "#ifgroup(#exists($.Tree.Root))": {
      "State": {
        "Value1": "#valueof($.Tree.Branch)",
        "Value2": "#valueof($.Tree.Flower)"
      }
    }
 }
}
 
Output:-
{ 
   "Result":{ 
      "Header":"JsonTransform"
   }
}


 Dynamic Properties

We can now create dynamic properties using the eval function. The function takes an expression as an argument.
 
Consider the following input:-
{
  "Tree": {   
    "Branch": "leaf",
    "Flower": "Rose"
  }
}
 
Transformer:-
{
  "Result": {
      "#eval(#valueof($.Tree.Flower))": "x"
  }
}
 
Output:-
{ 
   "Result":{ 
      "Rose":"x"
   }
} 
 

Schema Validation against multiple schemas using prefixes

 
A new feature to validate a JSON against multiple schemas has been introduced in the new Nuget 2.0.XX. This is to enable namespace based validation using prefixes like in XSD.
 
Below is a sample code which you need to write to validate a JSON against 2 schemas using prefixes:-
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using JUST;
using System.IO;

namespace JUST.Test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string inputJson = File.ReadAllText("ValidationInput.json")//read input from JSON file.;
            string schemaJsonX = File.ReadAllText("SchemaX.json")//read first schema from JSON file.;
            string schemaJsonY = File.ReadAllText("SchemaY.json")//read second input from JSON file.;

            JsonValidator validator = new JsonValidator(inputJson)//create instance of JsonValidator using the input.;
            validator.AddSchema("x", schemaJsonX)//Add first schema with prefix 'x'.;
            validator.AddSchema("y", schemaJsonY)//Add second schema with prefix 'y'.;

            validator.Validate();

        }
    }
}
 
In the above case if the validation is un-successful an exception will be thrown with the validation errors.
 
Consider the validation input:-
{
  "x.tree": { "x.branch": { "x.leaf": "1" } },
  "x.child": 1,
  "y.animal": 1
}

Schema X JSON:-
{
  "properties": {
    "tree": {
      "type": "object",
      "properties": {
        "branch": {
          "type": "object",
          "properties": {
            "leaf": { "type": "string" }
          }
        }
      }
    },
    "child": { "type": "string" }
  }
}
 
Schema Y JSON:-
{
  "properties": {
    "animal": { "type": "string" }
  }
}
 
The exception message thrown in the above case would be:-
Unhandled Exception: System.Exception: Invalid type. Expected String but got Integer. Path '['x.child']', line 3, position 14. AND Invalid type. Expected String but got Integer. Path '['y.animal']', line 4, position 15.

 

Splitting JSON into multiple JSON(s) based upon an array token

A JSON file containing an array can now be split into multiple JSON files, each representing a file for every array element.
 
Two new functions have been added for this purpose:-
 
  • public static IEnumerable<string> SplitJson(string input,string arrayPath)
  • public static IEnumerable<JObject> SplitJson(JObject input, string arrayPath)
 
Consider the input:-
{
  "cars": {
    "Ford": [
      {
        "model": "Taurus",
        "doors": 4
      },
      {
        "model": "Escort",
        "doors": 4
      },
      {
        "model": "Fiesta",
        "doors": 3
      },
      {
        "model": "Bronco",
        "doors": 5
      }
    ],
    "firstName": "John",
    "lastName": "Smith",
  }
}
 
Below is a sample code which splits the above input:-
string input = File.ReadAllText("Input.json");
List<string> outputs = JsonTransformer.SplitJson(input, "$.cars.Ford").ToList<string>();
 
The output will contain 4 JSON files:-
{"cars":{"Ford":{"model":"Taurus","doors":4},"firstName":"John","lastName":"Smith"}}
 
{"cars":{"Ford":{"model":"Escort","doors":4},"firstName":"John","lastName":"Smith"}}
 
{"cars":{"Ford":{"model":"Fiesta","doors":3},"firstName":"John","lastName":"Smith"}}
 
{"cars":{"Ford":{"model":"Bronco","doors":5},"firstName":"John","lastName":"Smith"}}

Transforming JSON to other data formats

JUST.NET can now transform JSON data into other generic formats too. All functions except the BULK FUNCTIONS are supported in this feature.

The #loop functions excepts an extra argument which defines the seperator between the individual records.
#loop(path,seaperator).
 
If the seperator is not defined, the default seperator used is NEWLINE.
 
A new class called DataTransformer has been introduced for this new feature.
 

Example for JSON to XML

Sample code to transform from JSON to XML:-
string input = File.ReadAllText("Input.json");
string transformer = File.ReadAllText("DataTransformer.xml");
string transformedString = DataTransformer.Transform(transformer, input);
 
Input.json:-
{
  "menu": {
    "id": {
      "file": "csv"
    },
    "value": {
      "Window": "popup"
    },
    "popup": {
      "menuitem": [
        {
          "value": "New",
          "onclick": {
            "action": "CreateNewDoc()"
          }
        },
        {
          "value": "Open",
          "onclick": "OpenDoc()"
        },
        {
          "value": "Close",
          "onclick": "CloseDoc()"
        }
      ]
    }
  },
  "x": [
    {
      "v": {
        "a": "a1,a2,a3",
        "b": "1",
        "c": "10"
      }
    },
    {
      "v": {
        "a": "b1,b2",
        "b": "2",
        "c": "20"
      }
    },
    {
      "v": {
        "a": "c1,c2,c3",
        "b": "3",
        "c": "30"
      }
    }
  ],
  "stringref": "thisisandveryunuasualandlongstring",
  "d": [ "one", "two", "three" ],
  "numbers": [ "1", "2", "3", "4", "5" ],
  "tree": {
    "branch": {
      "leaf": "green",
      "flower": "red",
      "bird": "crow"
    }
  },
  "Name": "Kari",
  "Surname": "Nordmann",
  "MiddleName": "Inger",
  "ContactInformation": "Karl johans gate:Oslo:88880000",
  "PersonalInformation": "45:Married:Norwegian",
  "AgeOfMother": "67",
  "AgeOfFather": "70",
  "BuyDate": "2017-04-10T11:36:39+03:00",
  "ExpireDate": "",
  "LogId": 5000510625
}

DataTransformer.xml:-
<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <root>
    <ifconditiontesttrue>#ifcondition(#valueof($.menu.id.file),csv,#valueof($.menu.value.Window),fail)</ifconditiontesttrue>
    <ifconditiontestfalse>#ifcondition(#valueof($.menu.id.file),xml,#valueof($.menu.value.Window),fail)</ifconditiontestfalse>
    <stringresult>
      <lastindexofand>#lastindexof(#valueof($.stringref),and)</lastindexofand>
      <firstindexofand>#firstindexof(#valueof($.stringref),and)</firstindexofand>
      <subsrting>#substring(#valueof($.stringref),8,10)</subsrting>
      <concat>#concat(#valueof($.menu.id.file),#valueof($.menu.value.Window))</concat>
    </stringresult>
    <mathresult>
      <add>#add(#valueof($.numbers[0]),3)</add>
      <subtract>#subtract(#valueof($.numbers[4]),#valueof($.numbers[0]))</subtract>
      <multiply>#multiply(2,#valueof($.numbers[2]))</multiply>
      <divide>#divide(9,3)</divide>
    </mathresult>
    <conacted>#concatall(#valueof($.d))</conacted>
    <sum>#sum(#valueof($.numbers))</sum>
    <avg>#average(#valueof($.numbers))</avg>
    <min>#min(#valueof($.numbers))</min>
    <max>#max(#valueof($.numbers))</max>
    <arrayconacted>#concatallatpath(#valueof($.x),$.v.a)</arrayconacted>
    <arraysum>#sumatpath(#valueof($.x),$.v.c)</arraysum>
    <arrayavg>#averageatpath(#valueof($.x),$.v.c)</arrayavg>
    <arraymin>#minatpath(#valueof($.x),$.v.b)</arraymin>
    <arraymax>#maxatpath(#valueof($.x),$.v.b)</arraymax>
  </root>
  <FullName>#concat(#concat(#concat(#valueof($.Name), ),#concat(#valueof($.MiddleName), )),#valueof($.Surname))</FullName>
  <Contact_Information>
    <City>#substring(#valueof($.ContactInformation),#add(#firstindexof(#valueof($.ContactInformation),:),1),#subtract(#subtract(#lastindexof(#valueof($.ContactInformation),:),#firstindexof(#valueof($.ContactInformation),:)),1))</City>
    <PhoneNumber>#substring(#valueof($.ContactInformation),#add(#lastindexof(#valueof($.ContactInformation),:),1),#subtract(#lastindexof(#valueof($.ContactInformation),),#lastindexof(#valueof($.ContactInformation),:)))</PhoneNumber>
    <Street_Name>#substring(#valueof($.ContactInformation),0,#firstindexof(#valueof($.ContactInformation),:))</Street_Name>
  </Contact_Information>
  <Personal_Information>
    <Age>#substring(#valueof($.PersonalInformation),0,#firstindexof(#valueof($.PersonalInformation),:))</Age>
    <Ethnicity>#substring(#valueof($.PersonalInformation),#add(#lastindexof(#valueof($.PersonalInformation),:),1),#subtract(#lastindexof(#valueof($.PersonalInformation),),#lastindexof(#valueof($.PersonalInformation),:)))</Ethnicity>
    <LogId>#valueof($.LogId)</LogId>
    <Civil_Status>#substring(#valueof($.PersonalInformation),#add(#firstindexof(#valueof($.PersonalInformation),:),1),#subtract(#subtract(#lastindexof(#valueof($.PersonalInformation),:),#firstindexof(#valueof($.PersonalInformation),:)),1))</Civil_Status>
  </Personal_Information>
  <Custom>#customfunction(JUST.NET.Test,JUST.NET.Test.Season.findseason,#valueof($.tree.branch.leaf),#valueof($.tree.branch.flower))</Custom>
  <iteration>
    "#loop($.numbers,<!--Record ends here-->)": {
    <Record>
      <CurrentValue>#currentvalue()</CurrentValue>
      <CurrentIndex>#currentindex()</CurrentIndex>
      <IsLast>#ifcondition(#currentindex(),#lastindex(),yes,no)</IsLast>
      <LastValue>#lastvalue()</LastValue>
      <SomeValue>#valueof($.LogId)</SomeValue>
    </Record>}
  </iteration> 
</root>
 
Output:-
<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <root>
    <ifconditiontesttrue>popup</ifconditiontesttrue>
    <ifconditiontestfalse>fail</ifconditiontestfalse>
    <stringresult>
      <lastindexofand>21</lastindexofand>
      <firstindexofand>6</firstindexofand>
      <subsrting>dveryunuas</subsrting>
      <concat>csvpopup</concat>
    </stringresult>
    <mathresult>
      <add>4</add>
      <subtract>4</subtract>
      <multiply>6</multiply>
      <divide>3</divide>
    </mathresult>
    <conacted>onetwothree</conacted>
    <sum>15</sum>
    <avg>3</avg>
    <min>1</min>
    <max>5</max>
    <arrayconacted>a1,a2,a3b1,b2c1,c2,c3</arrayconacted>
    <arraysum>60</arraysum>
    <arrayavg>20</arrayavg>
    <arraymin>1</arraymin>
    <arraymax>3</arraymax>
  </root>
  <FullName>Kari Inger Nordmann</FullName>
  <Contact_Information>
    <City>Oslo</City>
    <PhoneNumber>88880000</PhoneNumber>
    <Street_Name>Karl johans gate</Street_Name>
  </Contact_Information>
  <Personal_Information>
    <Age>45</Age>
    <Ethnicity>Norwegian</Ethnicity>
    <LogId>5000510625</LogId>
    <Civil_Status>Married</Civil_Status>
  </Personal_Information>
  <Custom>summer</Custom>
  <iteration>
    <Record>
      <CurrentValue>1</CurrentValue>
      <CurrentIndex>0</CurrentIndex>
      <IsLast>no</IsLast>
      <LastValue>5</LastValue>
      <SomeValue>5000510625</SomeValue>
    </Record><!--Record ends here-->
    <Record>
      <CurrentValue>2</CurrentValue>
      <CurrentIndex>1</CurrentIndex>
      <IsLast>no</IsLast>
      <LastValue>5</LastValue>
      <SomeValue>5000510625</SomeValue>
    </Record><!--Record ends here-->
    <Record>
      <CurrentValue>3</CurrentValue>
      <CurrentIndex>2</CurrentIndex>
      <IsLast>no</IsLast>
      <LastValue>5</LastValue>
      <SomeValue>5000510625</SomeValue>
    </Record><!--Record ends here-->
    <Record>
      <CurrentValue>4</CurrentValue>
      <CurrentIndex>3</CurrentIndex>
      <IsLast>no</IsLast>
      <LastValue>5</LastValue>
      <SomeValue>5000510625</SomeValue>
    </Record><!--Record ends here-->
    <Record>
      <CurrentValue>5</CurrentValue>
      <CurrentIndex>4</CurrentIndex>
      <IsLast>yes</IsLast>
      <LastValue>5</LastValue>
      <SomeValue>5000510625</SomeValue>
    </Record><!--Record ends here-->
  </iteration>
</root>
 

Example for JSON to CSV

Sample code to transform from JSON to CSV:-
string transformer = File.ReadAllText("Input.json");
string transformer = File.ReadAllText("DataTransformer.csv");
string transformedString = DataTransformer.Transform(transformer, input);
 
The input file is same as the xml example.
 
DataTransformer.csv:-
"#loop($.numbers)": {#currentvalue(),#currentindex(),#ifcondition(#currentindex(),#lastindex(),yes,no),#lastvalue(),#valueof($.LogId)}
 
Output:-
1,0,no,5,5000510625
2,1,no,5,5000510625
3,2,no,5,5000510625
4,3,no,5,5000510625
5,4,yes,5,5000510625

Link to Test Source Code (Source code also available now)

I have made a GitHub repository which contains a C# project and has various transformation scenarios on an input JSON file.

I hope you will have fun using JUST for transforming your JSON documents.

History

  1. First version of JUST.NET
  2. Minor correction in the output of the first transformer (valueof)
  3. Math function DEVIDE has been changed to DIVIDE after feedback
  4. Added link to GitHub repository for test project
  5. Added "Array looping" section
  6. Added "Calling custom functions" section
  7. Added "Nesting of functions" section
  8. Added "Multiple argument & constant functions" section
  9. Added "Schema Validation against multiple schemas using prefixes" section
  10. Added "Check for existence" section
  11. Added information about the dotnetcore nuget package.
  12. Added "Transforming JSON to other data formats" section.
  13. Added "Splitting JSON into multiple JSON(s) based upon an array token" section.
  14. Added "Nested array looping (looping within context)" functionality.
  15. Added "Array grouping" section.
  16. Added "Conditional transformation" & "Dynamic properties" sections.
  17. Added "Operators" section, information about source code and .NET standard package.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

JUST_Neeraj
Architect
Norway Norway
I have 14 years of IT experience on the .NET Stack. I am back-end\middleware expert focussing on databases, APIs and integration systems. I have been focussing a lot on AWS in the last 2 years.
https://github.com/WorkMaze

You may also be interested in...

Comments and Discussions

 
QuestionAggregate Functions & Dates Pin
GrafxCodeProject23-Nov-18 5:35
memberGrafxCodeProject23-Nov-18 5:35 
QuestionIs it possible to flatten/transform this json input to this result with JUST? Pin
OlkiDer9-Nov-18 0:46
memberOlkiDer9-Nov-18 0:46 
QuestionNewtonsoftjson reference version 10.0 Pin
Member 1352478421-Jul-18 4:33
memberMember 1352478421-Jul-18 4:33 
GeneralMessage Closed Pin
12-Jun-18 16:40
memberMember 1387027412-Jun-18 16:40 
QuestionGroup By Array and Aggregation Pin
GrafxCodeProject1-Jun-18 6:15
memberGrafxCodeProject1-Jun-18 6:15 
AnswerRe: Group By Array and Aggregation Pin
JUST_Neeraj3-Jun-18 3:47
professionalJUST_Neeraj3-Jun-18 3:47 
GeneralRe: Group By Array and Aggregation Pin
GrafxCodeProject4-Jun-18 7:55
memberGrafxCodeProject4-Jun-18 7:55 
GeneralRe: Group By Array and Aggregation Pin
JUST_Neeraj4-Jun-18 21:56
professionalJUST_Neeraj4-Jun-18 21:56 
QuestionLooping over array of objects? Pin
djay9924-May-18 23:33
memberdjay9924-May-18 23:33 
AnswerRe: Looping over array of objects? Pin
JUST_Neeraj3-Jun-18 4:09
professionalJUST_Neeraj3-Jun-18 4:09 
QuestionHow do you map enumvalues? Pin
Member 1158542514-Feb-18 1:12
memberMember 1158542514-Feb-18 1:12 
AnswerRe: How do you map enumvalues? Pin
JUST_Neeraj21-Mar-18 10:21
professionalJUST_Neeraj21-Mar-18 10:21 
QuestionIf there a way to use #ifgroup, #exists and #currentvalueatpath together inside an array? Pin
Per Schjetne NOR10-Feb-18 5:31
memberPer Schjetne NOR10-Feb-18 5:31 
AnswerRe: If there a way to use #ifgroup, #exists and #currentvalueatpath together inside an array? Pin
JUST_Neeraj21-Mar-18 10:19
professionalJUST_Neeraj21-Mar-18 10:19 
QuestionSo...the source code? Pin
MLansdaal8-Dec-17 13:53
memberMLansdaal8-Dec-17 13:53 
AnswerRe: So...the source code? Pin
JUST_Neeraj9-Dec-17 4:31
professionalJUST_Neeraj9-Dec-17 4:31 
GeneralRe: So...the source code? Pin
MLansdaal9-Dec-17 11:45
memberMLansdaal9-Dec-17 11:45 
GeneralRe: So...the source code? Pin
JUST_Neeraj12-Jun-18 11:46
professionalJUST_Neeraj12-Jun-18 11:46 
QuestionNode Deletion Pin
Member 1352478416-Nov-17 21:22
memberMember 1352478416-Nov-17 21:22 
AnswerRe: Node Deletion Pin
JUST_Neeraj17-Nov-17 8:00
professionalJUST_Neeraj17-Nov-17 8:00 
GeneralRe: Node Deletion Pin
Member 1352478417-Nov-17 12:09
memberMember 1352478417-Nov-17 12:09 
GeneralRe: Node Deletion Pin
JUST_Neeraj18-Nov-17 1:33
professionalJUST_Neeraj18-Nov-17 1:33 
QuestionIs there a way we can only apply if condition in JUST. The reason i am asking is because in else condition we are sending the default value which other side is not accepting? Pin
Member 1352478416-Nov-17 3:25
memberMember 1352478416-Nov-17 3:25 
AnswerRe: Is there a way we can only apply if condition in JUST. The reason i am asking is because in else condition we are sending the default value which other side is not accepting? Pin
JUST_Neeraj17-Nov-17 7:57
professionalJUST_Neeraj17-Nov-17 7:57 
GeneralRe: Is there a way we can only apply if condition in JUST. The reason i am asking is because in else condition we are sending the default value which other side is not accepting? Pin
Member 135247846-Dec-17 22:55
memberMember 135247846-Dec-17 22:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.181207.3 | Last Updated 11 Jun 2018
Article Copyright 2017 by JUST_Neeraj
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid