JSON JOLT常用示例整理
1、什么是jolt
Jolt是用Java编写的JSON到JSON转换库,其中指示如何转换的"specification"本身就是一个JSON文档。以下文档中,我统一以 Spec 代替如何转换的"specification"json文档。以LHS(left hand side)代表Spec json的keys,RHS(right hand side)代表Spec json的values。部分示例都是摘取于Jolt源代码注释文档。
用处:
- 将从ElasticSearch、MongoDb、Cassandra等等取出的数据转换后输出出来
- 从大型JSON文档中提取数据供自己使用
2、常用网站
-
Jolt GitHub: https://github.com/bazaarvoice/jolt (opens new window)
-
Jolt online demo: https://jolt-demo.appspot.com
3、转换示例
3.1 基础转换
字段key转换,若转换中没有该key值,可以给默认值,json spec如下
[ { "operation": "shift", "spec": { "user": { "username": ["NAME", "USERCODE"], "password": "PWD" } } }, { "operation": "default", "spec": { "SEX": "1" } } ]
3.2 字典判断转换
比如第三方传递过来的性别编码和自身系统的不一致,需要在json转换的时候进行转换
下面举例第三方返回的集合数据,进行数据转换
1、需要把性别编码
1.2.156.112604.1.2.5.2
—>0
男
1.2.156.112604.1.2.5.3
—>1
女2、把患者年龄的【岁】字去掉,保留数字
json spec如下
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"patName": "data[&1].patientName",
"patBirth": "data[&1].birthDay",
"patPhone": "data[&1].phone",
"patAddress": "data[&1].address",
"patIdCard": "data[&1].idCardNumber",
"patSexCode": {
"1.2.156.112604.1.2.5.2": {
//自身系统的性别未0 是男 ,1是女
"#0": "data[&3].sex"
},
"1.2.156.112604.1.2.5.3": {
"#1": "data[&3].sex"
}
},
"ageStr": {
"*岁": {
"$(0,1)": "data[&3].age"
}
}
}
}
}
},
{
"operation": "default",
"spec": {
"count": 0,
"status": true,
"data": []
}
}, {
"operation": "default",
"spec": {
"data[]": {
"*": {
"cardType": "1",
"patientStatus": 0,
"nation": "01"
}
}
}
}
]
3.3 单字段转数组
{
“name”:“arr[]”
}
3.4 集合LIST中判断取值
第三方传递过来的数据是一个集合数据包含了(门诊号、病历号等等),需要根据不同的key值进行判断取值
输入json
{
"patient": {
"classCode": "PAT",
"id": {
"item": [
{
"extension": "02",
"root": "1.2.156.112606.1.2.1.2"
},
{
"extension": "30341855",
"root": "1.2.156.112606.1.2.1.3"
},
{
"extension": "8016698",
"root": "1.2.156.112606.1.2.1.12"
},
{
"extension": "305247745248301056",
"root": "1.2.156.112606.1.2.1.13"
},
{
"extension": "",
"root": "1.2.156.112606.1.2.1.101"
},
{
"extension": "8016698",
"root": "1.2.156.112606.1.2.1.102"
},
{
"extension": "202300079171",
"root": "1.2.156.112606.1.2.1.103"
}
]
}
}
}
转换的json spec
[
{
"operation": "shift",
"spec": {
"patient": {
"id": {
"item": {
"*": {
"root": {
// 患者ID
"1.2.156.112606.1.2.1.3": {
"@(2,extension)": "patientId"
},
//住院号
"1.2.156.112606.1.2.1.12": {
"@(2,extension)": "inpatientNo"
},
//就诊标识
"1.2.156.112606.1.2.1.13": {
"@(2,extension)": "encounterId"
},
//门诊病历号
"1.2.156.112606.1.2.1.101": {
"@(2,extension)": "outpatientMedicalNo"
},
//住院病历号
"1.2.156.112606.1.2.1.102": {
"@(2,extension)": "inHospitalMedicalNo"
},
//病案号
"1.2.156.112606.1.2.1.103": {
"@(2,extension)": "bah"
}
}
}
}
}
}
}
}
]
3.5 取数组的第一个
获取数组的第一个对象值
输入json
{
"data": [
{
"name": "zhangsan",
"age": "22"
},
{
"name": "lisi",
"age": "33"
}
],
"status": "true"
}
转换json spec
[
{
"operation": "shift",
"spec": {
"data": {
"0": "nameObj"
}
}
}
]
3.6 多条件取值
满足下面条件
//a=1 赋值给字段c=123
//b=1 赋值给字段c=321
//如果都不满足给个默认值 c=888
输入json
{
"a":"1",
"b":"1"
}
转换json spec
[
{
"operation": "shift",
"spec": {
"a": {
"1": {
"#123": "tempArr[]"
}
},
"b": {
"1": {
"#321": "tempArr[]"
}
}
}
},
{
"operation": "shift",
"spec": {
"tempArr": {
"0": "c"
}
}
},
{
"operation": "default",
"spec": {
//如果上面都不匹配,给一个默认值
"c": "888"
}
}
]
3.7 字段字符串拼接
需要将下面的name1+name2+name3 拼接起来去掉为空的值
输入json
{
"Request": {
"Body": {
"nameObj": {
"name1": "hello",
"name2": "world",
"name3": "happy"
}
}
}
}
转换json spec
[
{
"operation": "shift",
"spec": {
"Request": {
"Body": {
"nameObj": {
"name1": "name1",
"name2": "name2",
"name3": "name3"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"name1": {
"null": null
},
"name2": {
"null": null
},
"name3": {
"null": null
},
"Request": {
"Body": {
"person": {
//直接拼接
"nameStr": "=concat(@(4,name1), @(4,name2), @(4,name3))",
//用-连接
"nameStr2": "=concat(@(4,name1),'-', @(4,name2),'-', @(4,name3))"
}
}
}
}
},
{
"operation": "remove",
"spec": {
// 指定要移除的字段的路径
"name1": "",
"name2": "",
"name3": ""
}
}
]