demo 解读
有了第一篇的基础,操作jolt已经不成问题,对于大部分json的mapping已经得心应手了,本片主要聚焦jolt除了json的mapping功能以外的其他功能。
数据相关 - List Functions
json input
{
"scores": [
4,
2,
8,
7,
5
]
}
json spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
// 计算数组长度
"numScores": "=size(@(1,scores))",
// 数组取头取尾
"firstScore": "=firstElement(@(1,scores))",
"lastScore": "=lastElement(@(1,scores))",
// 出不来值
"scoreAtMidPoint": "=elementAt(@(1,scores),2)",
// 数组排序
"sortedScores": "=sort(@(1,scores))" }
}
]
json output
{
"scores" : [ 4, 2, 8, 7, 5 ],
"numScores" : 5,
"firstScore" : 4,
"lastScore" : 5,
"sortedScores" : [ 2, 4, 5, 7, 8 ]
}
数学相关 - Math Functions
json input
{
"intData" : [ 2, 7, 5 ],
"doubleData" : [ 0.25, 1.5, 1 ],
"a" : 10,
"b" : 5,
"c" : 3,
"negative" : "-1.0"
}
json spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
//
// Sums
"sumIntData": "=intSum(@(1,intData))",
"sumLongData": "=intSum(@(1,intData))", // same as intSum but returns a Java Long
"sumDoubleData": "=doubleSum(@(1,doubleData))",
//
// Averages
"avgIntData" : "=avg(@(1,intData))", // note this returns a double
"avgDoubleData" : "=avg(@(1,doubleData))",
//
// Sort ascending
"sortedIntScores" : "=sort(@(1,intData))",
//
// Min, Max, Absolute Value
"minAB" : "=min(@(1,a),@(1,b))", // should be 5
"maxAB" : "=max(@(1,a),@(1,b))", // should be 10
"abs" : "=abs(@(1,negative))",
//
// Divide
"aDivB": "=divide(@(1,a),@(1,b))",
"aDivC": "=divide(@(1,a),@(1,c))", // will be 3.3333
//
// Divide and Round : decimal point to round to is first param
"aDivCRounded4": "=divideAndRound(4,@(1,a),@(1,c))"
}
}
]
json output
{
"intData" : [ 2, 7, 5 ],
"doubleData" : [ 0.25, 1.5, 1 ],
"a" : 10,
"b" : 5,
"c" : 3,
"negative" : "-1.0",
"sumIntData" : 14,
"sumLongData" : 14,
"sumDoubleData" : 2.75,
"avgIntData" : 4.666666666666667,
"avgDoubleData" : 0.9166666666666666,
"sortedIntScores" : [ 2, 5, 7 ],
"minAB" : 5,
"maxAB" : 10,
"abs" : 1.0,
"aDivB" : 2.0,
"aDivC" : 3.3333333333333335,
"aDivCRounded4" : 3.3333
}
类型转换 - Type Conversion
json input
{
"happy": "true",
"meh": "meh",
"answer": 42,
"statistics" : [
{
"id" : "A",
"min" : "2.0",
"max" : "10.0",
"avg" : "7.9"
},
{
"min" : "6",
"max" : "6",
"avg" : "6"
},
{
"id" : "C"
}
]
}
json spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
// convert strings to a boolean, case-insensitive
"happy": "=toBoolean",
// meh might not be a boolean, so if it does not evaluate to a boolean
// then default in a boolean of false
"meh": ["=toBoolean", false],
//
// convert the number to a String
"answer": "=toString",
//
//
// Here we want to do some type conversions, and specify
// default values for missing data.
"statistics": {
"*": {
// Overwrite min, max, and avg by type converting them.
// If they don't exist, then fill in default values.
"min": ["=toInteger", 0],
"max": ["=toInteger", null],
"avg": ["=toDouble", null],
//
// In this example, min, max, and average "overwrite"
// is the right thing to do (why the operation is
// "modify-overwrite-beta")
//
// Note that the 2nd item in the list does not have and "id"
// field.
//
// Is this instane, we don't actually want to "overwrite" the "id" it exists
// but we do want to apply a default if it does not.
// In the input, the 2nd item in the "statistics" array is missing its id.
//
// The solution is to apply a "node level" change of behavior from
// "overwrite" to "define", so that we will only fill in the id if it is missing.
//
// To do this we prefix the ke, "id", with an underscore, "_".
"_id": "UNKNOWN"
}
}
}
}
]
json output
{
"happy" : true,
"meh" : false,
"answer" : "42",
"statistics" : [ {
"id" : "A",
"min" : 2,
"max" : 10,
"avg" : 7.9
}, {
"min" : 6,
"max" : 6,
"avg" : 6.0,
"id" : "UNKNOWN"
}, {
"id" : "C",
"min" : 0,
"max" : null,
"avg" : null
} ]
}
字符串连接 - String Concatenation
json input
{
"x": [ 3, 2, 1, "go" ],
"small": "small",
"BIG": "BIG",
"people": [
{
"firstName": "Bob",
"lastName": "Smith",
"address": {
"state": null
}
},
{
"firstName": "Sterling",
"lastName": "Archer"
}
]
}
json spec
[
{
"operation": "modify-default-beta",
"spec": {
// String join the values in the array x, with a comma and a space
"y": "=join(',',@(1,x))",
"z": "=join(' ',@(1,x))",
//
// make small big, and make big small
"small_toUpper": "=toUpper(@(1,small))",
"BIG_toLower": "=toLower(@(1,BIG))",
//
// Here we are dealing with an array of people objects.
// We want to build a fullName for them, and default them
// to 'live' in Texas, only if they don't already have an address defined.
"people": {
"*": {
// build the fullName from the first and last names
//
// @(1,firstName)
// - is "evaluated" before being passed to the =concat function.
// - means go up two levels, and then come back down and grab
// the value from firstName
"fullName": "=concat(@(1,firstName),' ',@(1,lastName))",
//
// Suffix of "?" means only match if the input actually has an "address"
"address?": {
// The transform "modify-default-beta" will only match if the
// "left hand side" does not exist or is null
"state": "Texas"
}
}
}
}
}
]
json output
{
"x" : [ 3, 2, 1, "go" ],
"small" : "small",
"BIG" : "BIG",
"people" : [ {
"firstName" : "Bob",
"lastName" : "Smith",
"address" : {
"state" : "Texas"
},
"fullName" : "Bob Smith"
}, {
"firstName" : "Sterling",
"lastName" : "Archer",
"fullName" : "Sterling Archer"
} ],
"y" : "3,2,1,go",
"z" : "3 2 1 go",
"small_toUpper" : "SMALL",
"BIG_toLower" : "big"
}