探索 JOLT 函数进行 JSON 到 JSON 转换:概述

huangapple go评论67阅读模式
英文:

Exploring JOLT functions for JSON to JSON transformations: An overview

问题

有人能简要解释一下JOLT转换库中用于JSON到JSON转换的不同函数吗?

JOLT函数的简要解释:

  • 类似于:
    函数名称 - 其作用 - 参数
英文:

Can someone give a brief explanation of different functions in the JOLT transformation library for JSON to JSON transformations?

A brief explanation of JOLT function.


  • Like :-
    Function name - what it does -- its arguments

答案1

得分: 1

以下是JOLT函数的简要概述:

String

  • toLower - 将字符串转换为小写 -- 可接受一个字符串参数,如果有多个字符串参数,则返回一个数组。
  • toUpper - 将字符串转换为大写 -- 可接受一个字符串参数,如果有多个字符串参数,则返回一个数组。
  • concat - 连接字符串 -- 可接受多个要连接的字符串参数。
  • join - 使用第一个参数中指定的字符串连接字符串 -- 可接受多个字符串参数或者可以传递一个数组。
  • split - 根据指定的分隔符将字符串拆分为数组 -- 可接受两个参数:第一个是分隔符,第二个是字符串。
  • substring - 创建子字符串 -- 可接受三个参数:第一个是字符串,第二个是起始索引,第三个是结束索引。
  • trim - 去除字符串前后的额外空格 -- 可接受一个字符串参数,如果有多个字符串参数,则返回一个数组。
  • leftPad 和 rightPad - 在字符串的开头或结尾添加字符以达到所需的长度 -- 可接受三个参数:第一个是输入字符串,第二个是所需长度,第三个是要添加的字符。

Type

  • toInteger - 转换为整数
  • toDouble - 转换为双精度浮点数
  • toLong - 转换为长整数
  • toBoolean - 转换为布尔值
  • toString - 转换为字符串
  • squashNulls 和 recursivelySquashNulls - 关于recursivelySquashNulls和squashNulls函数,它们仅适用于对象和列表,用于删除具有null值的字段,但是recursivelySquashNulls将查看对象或列表下面的所有级别,而squashNulls将只查看下面的一级。
  • size - 返回字符串的大小

List

  • firstElement - 从数组返回第一个元素 -- 仅接受一个参数,即数组。
  • lastElement - 从数组返回最后一个元素 -- 仅接受一个参数,即数组。
  • elementAt - 返回指定索引处的元素 -- 接受两个参数:第一个是数组,第二个是索引。
  • toList - 返回包含给定字符串的数组 -- 仅接受一个参数,即字符串。
  • sort - 按字典顺序对数组进行排序 -- 仅接受一个参数,即数组。

Number

  • min - 返回数组或值中的最小值 -- 接受多个值参数或一个数组。
  • max - 返回数组或值中的最大值 -- 接受多个值参数或一个数组。
  • abs - 返回绝对值 -- 仅接受一个整数值参数。
  • avg - 计算数组或值的平均值 -- 接受数组或多个值参数。
  • intSum - 求和数组或值 -- 接受数组或多个值参数。
  • doubleSum - 求和数组或值并返回双精度浮点数 -- 接受数组或多个值参数。
  • longSum - 求和数组或值并返回长整数 -- 接受数组或多个值参数。
  • intSubtract - 减去两个值 -- 接受两个值参数。
  • doubleSubtract - 减去两个值 -- 接受两个值参数。
  • longSubtract - 减去两个值 -- 接受两个值参数。
  • divide - 除以两个值 -- 接受两个值参数。
  • divideAndRound - 除以值并四舍五入到指定的小数位数 - 接受三个参数:第一个是小数位数,第二个是被除数,第三个是除数。
英文:

Brief overview of JOLT functions

String

  • toLower - converts the string into lowercase -- can accept one argument that is string and if there are multiple args of string then it return an array.
  • toUpper - converts the string into uppercase -- can accept one argument that is string and if there are multiple args of string then it return an array.
  • concat - concats strings -- accepts multiple args of string to be concatenated.
  • join - Join the string with the string given in first arg -- accepts multiple args of string or you can pass an array.
  • split - split the string into an array based on the given delimiter -- accepts two args : first - delimiter, second - string
  • substring - to create a substring -- accepts three args: first - string, second-start index, third - end index
  • trim - trims the extra space before and after the string -- can accept one argument that is string and if there are multiple args of string then it return an array.
  • leftPad e rightPad - adds the characters to the string on start or end to make it to the desired length -- accepts three args: first - input string,
    second - string desired length, third - character that is to be added

Type

  • toInteger - converts to int
  • toDouble - converts to double
  • toLong - converts to long
  • toBoolean - converts to int
  • toString - converts to string
  • squashNulls and recursivelySquashNulls - Regarding the recursivelySquashNulls and squashNulls functions, both are applicable only to objects and lists and serve to remove fields
    with null values, however while recursivelySquashNulls will look at all levels below the object or list, squashNulls will look just 1 level below.
  • size - return the size of string

List

  • firstElement - returns the first element from the array -- accepts only one arg i.e. array
  • lastElement - returns the last element from the array -- accepts only one arg i.e. array
  • elementAt - return the element at index -- accepts two args: first - array, second - index
  • toList - return an array containing the given string -- accepts only one arg that is string
  • sort - sorts the array in lexicographical order -- accepts only one arg i.e. array

Number

  • min - return min from the array or values -- accepts multple args of vals or an array.
  • max - returns max from the array or values -- accepts multple args of vals or an array.
  • abs - give the absolute value -- accepts only one arg i.e. int val
  • avg - calculates the avg from the array or vals-- accepts array or multiple vals
  • intSum - sums the array or values -- accepts array or multiple vals
  • doubleSum - sums the array or values returns the value in double -- accepts array or multiple vals
  • longSum - sums the array or values returns the value in long -- accepts array or multiple vals
  • intSubtract - subtracts two value -- accepts two arg - first and second val
  • doubleSubtract -- accepts two arg - first and second val
  • longSubtract -- accepts two arg - first and second val
  • divide - divides two value -- accepts two arg - first and second val
  • divideAndRound - divides the value and round it upto the given decimal places - accepts three arg: first - the decimal places, second - dividend, third - divisor

答案2

得分: 0

以下是您要翻译的内容:

JSON 输入:

{
  "STRING": {
    "product": "Product A",
    "company": "company a",
    "value": "100",
    "measureWithSpaces": "  10 meters "
  },
  "NUMBER": {
    "array": [ 3, 5, 2, 7, 1 ],
    "negativeValue": -100,
    "positiveValue": 50
  },
  "TYPE": {
    "value": 10.5,
    "stringBoolean": "true",
    "objectWithNull": {
      "fielWithValue": "ABC",
      "nullField": null
    }
  },
  "LIST": {
    "array": [ "c", "t", "m", "a" ],
    "stringField": "123"
  }
}

Jolt 规范:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "STRING": {
        "product": "=toLower(@(1,product))",
        "company": "=toUpper(@(1,company))",
        "product_company": "=concat(@(1,product),'_',@(1,company))",
        "joinProductCompany": "=join(' - ',@(1,product),@(1,company))",
        "splitProductCompany": "=split('[-]',@(1,joinProductCompany))",
        "substringProduct": "=substring(@(1,product),0,4)",
        "value": "=leftPad(@(1,value),6,'A')",
        "measure": "=trim(@(1,measureWithSpaces))"
      },
      "NUMBER": {
        "minArray": "=min(@(1,array))",
        "maxArray": "=max(@(1,array))",
        "absoluteValue": "=abs(@(1,negativeValue))",
        "averageArray": "=avg(@(1,array))",
        "sumArray": "=intSum(@(1,array))",
        "subtrArray": "=intSubtract(@(1,positiveValue), 20)",
        "division": "=divide(@(1,positiveValue),2)",
        "divisionRound": "=divideAndRound(3,@(1,positiveValue),3)"
      },
      "TYPE": {
        "integerValue": "=toInteger(@(1,value))",
        "booleano": "=toBoolean(@(1,stringBoolean))",
        "stringValue": "=toString(@(1,value))",
        "stringBoolean": "=size",
        "objectWithNull": "=recursivelySquashNulls"
      },
      "LIST": {
        "arrayFirstItem": "=firstElement(@(1,array))",
        "arrayLastItem": "=lastElement(@(1,array))",
        "arrayElement": "=elementAt(@(1,array),2)",
        "fieldToList": "=toList(@(1,stringField))",
        "orderedArray": "=sort(@(1,array))"
      }
    }
  }
]

JSON 输出:

{
  "STRING": {
    "product": "product a",
    "company": "COMPANY A",
    "value": "AAA100",
    "measureWithSpaces": "10 meters",
    "product_company": "product a_COMPANY A",
    "joinProductCompany": "product a - COMPANY A",
    "splitProductCompany": [
      "product a ",
      " COMPANY A"
    ],
    "substringProduct": "prod",
    "measure": "10 meters"
  },
  "NUMBER": {
    "array": [ 3, 5, 2, 7, 1 ],
    "negativeValue": -100,
    "positiveValue": 50,
    "minArray": 1,
    "maxArray": 7,
    "absoluteValue": 100,
    "averageArray": 3.6,
    "sumArray": 18,
    "subtrArray": 30,
    "division": 25,
    "divisionRound": 16.667
  },
  "TYPE": {
    "value": 10.5,
    "stringBoolean": 4,
    "objectWithNull": {
      "fielWithValue": "ABC"
    },
    "integerValue": 10,
    "booleano": true,
    "stringValue": "10.5"
  },
  "LIST": {
    "array": [
      "c",
      "t",
      "m",
      "a"
    ],
    "stringField": "123",
    "arrayFirstItem": "c",
    "arrayLastItem": "a",
    "fieldToList": [
      "123"
    ],
    "orderedArray": [ "a", "c", "m", "t" ]
  }
}

请注意,我已将 HTML 实体编码(如 ")替换为原始字符。

英文:

Here is example of each function

JSON input

{
  "STRING": {
    "product": "Product A",
    "company": "company a",
    "value": "100",
    "measureWithSpaces": "  10 meters "
  },
  "NUMBER": {
    "array": [ 3, 5, 2, 7, 1 ],
    "negativeValue": -100,
    "positiveValue": 50
  },
  "TYPE": {
    "value": 10.5,
    "stringBoolean": "true",
    "objectWithNull": {
      "fielWithValue": "ABC",
      "nullField": null
    }
  },
  "LIST": {
    "array": [ "c", "t", "m", "a" ],
    "stringField": "123"
  }
}

Jolt Specification

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "STRING": {
        "product": "=toLower(@(1,product))",
        "company": "=toUpper(@(1,company))",
        "product_company": "=concat(@(1,product),'_',@(1,company))",
        "joinProductCompany": "=join(' - ',@(1,product),@(1,company))",
        "splitProductCompany": "=split('[-]',@(1,joinProductCompany))",
        "substringProduct": "=substring(@(1,product),0,4)",
        "value": "=leftPad(@(1,value),6,'A')",
        "measure": "=trim(@(1,measureWithSpaces))"
      },
      "NUMBER": {
        "minArray": "=min(@(1,array))",
        "maxArray": "=max(@(1,array))",
        "absoluteValue": "=abs(@(1,negativeValue))",
        "averageArray": "=avg(@(1,array))",
        "sumArray": "=intSum(@(1,array))",
        "subtrArray": "=intSubtract(@(1,positiveValue), 20)",
        "division": "=divide(@(1,positiveValue),2)",
        "divisionRound": "=divideAndRound(3,@(1,positiveValue),3)"
      },
      "TYPE": {
        "integerValue": "=toInteger(@(1,value))",
        "booleano": "=toBoolean(@(1,stringBoolean))",
        "stringValue": "=toString(@(1,value))",
        "stringBoolean": "=size",
        "objectWithNull": "=recursivelySquashNulls"
      },
      "LIST": {
        "arrayFirstItem": "=firstElement(@(1,array))",
        "arrayLastItem": "=lastElement(@(1,array))",
        "arrayElement": "=elementAt(@(1,array),2)",
        "fieldToList": "=toList(@(1,stringField))",
        "orderedArray": "=sort(@(1,array))"
      }
    }
  }
]

JSON output

{
  "STRING": {
    "product": "product a",
    "company": "COMPANY A",
    "value": "AAA100",
    "measureWithSpaces": "  10 meters ",
    "product_company": "product a_COMPANY A",
    "joinProductCompany": "product a - COMPANY A",
    "splitProductCompany": [
      "product a ",
      " COMPANY A"
    ],
    "substringProduct": "prod",
    "measure": "10 meters"
  },
  "NUMBER": {
    "array": [ 3, 5, 2, 7, 1 ],
    "negativeValue": -100,
    "positiveValue": 50,
    "minArray": 1,
    "maxArray": 7,
    "absoluteValue": 100,
    "averageArray": 3.6,
    "sumArray": 18,
    "subtrArray": 30,
    "division": 25,
    "divisionRound": 16.667
  },
  "TYPE": {
    "value": 10.5,
    "stringBoolean": 4,
    "objectWithNull": {
      "fielWithValue": "ABC"
    },
    "integerValue": 10,
    "booleano": true,
    "stringValue": "10.5"
  },
  "LIST": {
    "array": [
      "c",
      "t",
      "m",
      "a"
    ],
    "stringField": "123",
    "arrayFirstItem": "c",
    "arrayLastItem": "a",
    "fieldToList": [
      "123"
    ],
    "orderedArray": [ "a", "c", "m", "t" ]
  }
}

huangapple
  • 本文由 发表于 2023年5月22日 15:07:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定