Posted In: JSON

How to transform JSON to another JSON format using JOLT

 

This example uses JOLT library

 
You need input json and spec json

package com.javausecase.jolt;

import java.util.List;

import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.JsonUtils;

public class App {
	public static void main(String[] args) {
		List specJSON = JsonUtils.classpathToList(
		        "/com/javausecase/jolt/spec.json");
		Chainr chainr = Chainr.fromSpec(specJSON);

		Object inputJSON = JsonUtils.classpathToObject(
		        "/com/javausecase/jolt/input.json");

		Object transformedOutput = chainr.transform(inputJSON);
		System.out.println(
		        JsonUtils.toJsonString(transformedOutput));
	}

}

 
It has following Transforms

shift       : copy data from the input tree and put it the output tree
default     : apply default values to the tree
remove      : remove data from the tree
sort        : sort the Map key values alphabetically ( for debugging and human readability )
cardinality : "fix" the cardinality of input data.  Eg, the "urls" element is usually a List, but if there is only one, then it is a String

 

Samples using “shift”

Input
{
	"portfolio": {
		"stock": "infosys",
		"quantity": "100",
		"buy": "1011",
		"profit": "10000"
	}
}


Spec
[
  {
    "operation": "shift",
    "spec": {
      "portfolio": "portfolioNew"
    }
  }
]


Output
{
  "portfolioNew" : {
    "stock" : "infosys",
    "quantity" : "100",
    "buy" : "1011",
    "profit" : "10000"
  }
}

 

Input
{
	"portfolio": {
		"stock": "infosys",
		"quantity": "100",
		"buy": "1011",
		"profit": "10000"
	}
}


Spec
[
  {
    "operation": "shift",
    "spec": {
      "portfolio": {
        "stock": "stock"
      }
    }
  }
]


Output
{
  "stock" : "infosys"
}

 

Input
{
	"portfolio": [
		{
			"stock": "Infosys",
			"quantity": "100",
			"buy": "1011",
			"profit": "10000"
		},
		{
			"stock": "State Bank of India",
			"quantity": "1000",
			"buy": "204",
			"profit": "20000"
		},
		{
			"stock": "Reliance Industries",
			"quantity": "100",
			"buy": "1200",
			"profit": "12000"
		}
	]
}


Spec
[
  {
    "operation": "shift",
    "spec": {
      "portfolio": {
        "*": {
          "stock": "stock"
        }
      }
    }
  }
]


Output
{
  "stock" : [ "Infosys", "State Bank of India", "Reliance Industries" ]
}

 

Input
{
	"portfolio": [
		{
			"stock": "Infosys",
			"quantity": "100",
			"buy": "1011",
			"profit": "10000"
		},
		{
			"stock": "State Bank of India",
			"quantity": "1000",
			"buy": "204",
			"profit": "20000"
		},
		{
			"stock": "Reliance Industries",
			"quantity": "100",
			"buy": "1200",
			"profit": "12000"
		}
	]
}


Spec
[
  {
    "operation": "shift",
    "spec": {
      "portfolio": {
        "*": {
          "stock": "stock-&1-id"
        }
      }
    }
  }
]


Output
{
  "stock-0-id" : "Infosys",
  "stock-1-id" : "State Bank of India",
  "stock-2-id" : "Reliance Industries"
}

 
 

Samples using “remove”

 

Input
{
	"portfolio": {
		"stock": "Infosys",
		"quantity": "100",
		"buy": "1011",
		"profit": "10000"
	}
}


Spec
[
  {
    "operation": "shift",
    "spec": {
      "portfolio": "portfolio"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "portfolio": {
        "profit": ""
      }
    }
  }
]


Output
{
  "portfolio" : {
    "stock" : "Infosys",
    "quantity" : "100",
    "buy" : "1011"
  }
}

 

Input
{
	"portfolio": [
		{
			"stock": "Infosys",
			"quantity": "100",
			"buy": "1011",
			"profit": "10000"
		},
		{
			"stock": "State Bank of India",
			"quantity": "1000",
			"buy": "204",
			"profit": "20000"
		},
		{
			"stock": "Reliance Industries",
			"quantity": "100",
			"buy": "1200",
			"profit": "12000"
		}
	]
}


Spec
[
  {
    "operation": "shift",
    "spec": {
      "portfolio": "portfolio"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "portfolio": {
        "*": {
          "profit": ""
        }
      }
    }
  }
]

Output
{
  "portfolio" : [ {
    "stock" : "Infosys",
    "quantity" : "100",
    "buy" : "1011"
  }, {
    "stock" : "State Bank of India",
    "quantity" : "1000",
    "buy" : "204"
  }, {
    "stock" : "Reliance Industries",
    "quantity" : "100",
    "buy" : "1200"
  } ]
}

 

To try this online use http://jolt-demo.appspot.com/#inception

 

Tags: , ,

by , on February 24th, 2017

  • Anusha Hegde

    I’m able to successfully apply specs to input json files. But, how to apply jolt specs to paginated output in Spring Data Elastic?

    • Anusha Hegde

      Okay, I got this! From the paginated output, get the page.getContent(). This will give a list on which the JOLT specs can be applied.

  • Categories