PROSE – Json Transformation

PROSE – Json Transformation

Overview

Transformation.Json transforms the structure of Json using input/output examples.

The Usage page and the Sample Project illustrate the API usage.

Example Transformation

Given an example to transform this input Json:

{
  "datatype": "local",
  "data": [
    {
      "Name": "John",
      "status": "To Be Processed",
      "LastUpdatedDate": "2013-05-31 08:40:55.0"
    },
    {
      "Name": "Paul",
      "status": "To Be Processed",
      "LastUpdatedDate": "2013-06-02 16:03:00.0"
    }
  ]
}

into this output Json:

[
  {
    "John" : "To Be Processed"
  },
  {
    "Paul" : "To Be Processed"
  }
]

Transformation.Json will generate a program to perform the same transformation given any other similar and larger input Json. For example, the learned program transforms this input:

{
  "datatype": "local",
  "data": [
    {
      "Name": "John",
      "status": "To Be Processed",
      "LastUpdatedDate": "2013-05-31 08:40:55.0"
    },
    {
      "Name": "Paul",
      "status": "To Be Processed",
      "LastUpdatedDate": "2013-06-02 16:03:00.0"
    },
    {
      "Name": "Alice",
      "status": "Finished",
      "LastUpdatedDate": "2013-07-02 12:04:00.0"
    }
  ]
}

into this output:

[
  {
    "John" : "To Be Processed"
  }, 
  { 
    "Paul" : "To Be Processed"
  }, 
  { 
    "Alice" : "Finished"
  } 
]

People

Usage

The main entry point is Session class’s Learn() method, which returns a Program object. The Program’s key method is Run() that executes the program on an input Json to obtain the transformed output Json.

Other important methods are Serialize() and Deserialize() to serialize and deserialize Program object.

The Sample Project illustrates our API usage.

Basic Usage

The following snippet illustrates a learning session to generate a Json transformation program from the example:

// The training input file, which is a small prefix of the input file.
JToken trainInput = JToken.Parse(@"
{
  ""datatype"": ""local"",
  ""data"": [
    {
      ""Name"": ""John"",
      ""status"": ""To Be Processed"",
      ""LastUpdatedDate"": ""2013-05-31 08:40:55.0""
    },
    {
      ""Name"": ""Paul"",
      ""status"": ""To Be Processed"",
      ""LastUpdatedDate"": ""2013-06-02 16:03:00.0""
    }
  ]
}");

// The training output file, which is the desired output for the training input.
JToken trainOutput = JToken.Parse(@"
[
    {
      ""John"" : ""To Be Processed""
    },
    {
      ""Paul"" : ""To Be Processed""
    }
]");

var session = new Session();
session.Constraints.Add(new Example(trainInput, trainOutput));
Program program = session.Learn();

Serializing/Deserializing a Program

The Transformation.Json.Program.Serialize() method serializes the learned program to a string.
The Transformation.Json.Loader.Instance.Load() method deserializes the program text to a program.

// program was learned previously
string progText = program.Serialize();
Program loadProg = Loader.Instance.Load(progText);

Executing a Program

Use the Run() method to obtain the transformed Json output:

JToken input = JToken.Parse(@"
{
  ""datatype"": ""local"",
  ""data"": [
    {
      ""Name"": ""John"",
      ""status"": ""To Be Processed"",
      ""LastUpdatedDate"": ""2013-05-31 08:40:55.0""
    },
    {
      ""Name"": ""Paul"",
      ""status"": ""To Be Processed"",
      ""LastUpdatedDate"": ""2013-06-02 16:03:00.0""
    },
    {
      ""Name"": ""Alice"",
      ""status"": ""Finished"",
      ""LastUpdatedDate"": ""2013-07-02 12:04:00.0""
    }
  ]
}");

JToken output = program.Run(input);

Output is:

[
  {
    "John" : "To Be Processed"
  },
  {
    "Paul" : "To Be Processed"
  },
  {
    "Alice" : "Finished"
  }
]