JUST Transform Your JSON

In this post we are going to use JUST.NET library to transform JSON documents to another JSON.

Let’s create a console app with visual studio:

Install the Nuget Package:

Install-Package JUST.NET

Or

Learn more about JUST: https://github.com/WorkMaze/JUST.net

Though JUST provide more transformation functions but in this post we are going to transform JSON to JSON with below functions:

  • valueof(property)
  • loop(path)
  • currentvalueatpath(path)
  • substring(input string, start index, length)
  • ifcondition(condition expression, evaluation expression, true result, false result)
  • existsandnotempty(path)
  • currentindex()
  • lastindex()
  • lastvalue()

Every JUST function starts with “#” character.

Input JSON:

{
  "squadName": "Super hero squad",
  "homeTown": "Metro City",
  "formed": 2016,
  "secretBase": "Super tower",
  "active": true,
  "members": [
    {
      "name": "Molecule Man",
      "age": 29,
      "secretIdentity": "Dan Jukes",
      "powers": [
        "Radiation resistance",
        "Turning tiny",
        "Radiation blast"
      ]
    },
    {
      "name": "Madame Uppercut",
      "age": 39,
      "secretIdentity": "Jane Wilson",
      "powers": [
        "Million tonne punch",
        "Damage resistance",
        "Superhuman reflexes"
      ]
    },
    {
      "name": "Eternal Flame",
      "age": 34,
      "secretIdentity": "",
      "powers": [
        "Immortality",
        "Heat Immunity",
        "Inferno",
        "Teleportation",
        "Interdimensional travel"
      ]
    }
  ]
}

Transformer JSON Template:

{
  "data": {
    "squadName": "#valueof($.squadName)",
    "homeTown": "#valueof($.homeTown)",
    "isactive": "#valueof($.active)",
    "teams": {
      "#loop($.members,)": {
        "fullname": "#currentvalueatpath($.name)",
        "firstname": "#substring(#currentvalueatpath($.name),0,8)",
        "age": "#currentvalueatpath($.age)",
        "identity": "#ifcondition(#existsandnotempty($.secretIdentity),true,#currentvalueatpath($.secretIdentity),#valueof(null))",
        "powers": {
          "#loop($.powers)": {
            "CurrentIndex": "#currentindex()",
            "title": "#currentvalue()",
            "IsLast": "#ifcondition(#currentindex(),#lastindex(),yes,no)",
            "LastValue": "#lastvalue()"
          }
        },
        "personalInformation": {
          "addressline1": null,
          "addressline2": null,
          "cell": null
        }
      }
    }
  }
}

JSONTransformation:

try
{
    //JSON-Input
    string _jsonInputData = await File.ReadAllTextAsync(@"JSONFiles/sample1.json");

    //Transformation-Template
    string _jsonTransformer = await File.ReadAllTextAsync(@"JUSTTemplates/template1.json");

    //JSON-JSON-Transformation
    var result1 = new JsonTransformer().Transform(_jsonTransformer, _jsonInputData);

    Console.WriteLine(result1);

    WriteToFile(DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_sample.json", result1);
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

Output JSON:

{
  "data": {
    "squadName": "Super hero squad",
    "homeTown": "Metro City",
    "isactive": true,
    "teams": [
      {
        "fullname": "Molecule Man",
        "firstname": "Molecule",
        "age": "29",
        "identity": "Dan Jukes",
        "powers": [
          {
            "CurrentIndex": 0,
            "title": "Radiation resistance",
            "IsLast": "no",
            "LastValue": "Radiation blast"
          },
          {
            "CurrentIndex": 1,
            "title": "Turning tiny",
            "IsLast": "no",
            "LastValue": "Radiation blast"
          },
          {
            "CurrentIndex": 2,
            "title": "Radiation blast",
            "IsLast": "yes",
            "LastValue": "Radiation blast"
          }
        ],
        "personalInformation": {
          "addressline1": null,
          "addressline2": null,
          "cell": null
        }
      },
      {
        "fullname": "Madame Uppercut",
        "firstname": "Madame U",
        "age": "39",
        "identity": "Jane Wilson",
        "powers": [
          {
            "CurrentIndex": 0,
            "title": "Million tonne punch",
            "IsLast": "no",
            "LastValue": "Superhuman reflexes"
          },
          {
            "CurrentIndex": 1,
            "title": "Damage resistance",
            "IsLast": "no",
            "LastValue": "Superhuman reflexes"
          },
          {
            "CurrentIndex": 2,
            "title": "Superhuman reflexes",
            "IsLast": "yes",
            "LastValue": "Superhuman reflexes"
          }
        ],
        "personalInformation": {
          "addressline1": null,
          "addressline2": null,
          "cell": null
        }
      },
      {
        "fullname": "Eternal Flame",
        "firstname": "Eternal ",
        "age": 34,
        "identity": null,
        "powers": [
          {
            "CurrentIndex": 0,
            "title": "Immortality",
            "IsLast": "no",
            "LastValue": "Interdimensional travel"
          },
          {
            "CurrentIndex": 1,
            "title": "Heat Immunity",
            "IsLast": "no",
            "LastValue": "Interdimensional travel"
          },
          {
            "CurrentIndex": 2,
            "title": "Inferno",
            "IsLast": "no",
            "LastValue": "Interdimensional travel"
          },
          {
            "CurrentIndex": 3,
            "title": "Teleportation",
            "IsLast": "no",
            "LastValue": "Interdimensional travel"
          },
          {
            "CurrentIndex": 4,
            "title": "Interdimensional travel",
            "IsLast": "yes",
            "LastValue": "Interdimensional travel"
          }
        ],
        "personalInformation": {
          "addressline1": null,
          "addressline2": null,
          "cell": null
        }
      }
    ]
  }
}

Download/clone full source code from @github, Hope this will help 🙂

Author:

Since March 2011, have 8+ years of professional experience on software development, currently working as Senior Software Engineer at s3 Innovate Pte Ltd.

Leave a Reply

Your email address will not be published.