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": ""
}
}
]
3.8 去除数组元素为null
输入input
{
"operatingRoomList": [
null,
{
"deptCode": "111",
"deptName": "AAA"
},
null,
{
"deptCode": "222",
"deptName": "bbb"
}
]
}
json spec
[
{
"operation": "shift",
"spec": {
"operatingRoomList": {
"*": {
// 当值不为null时进行映射
"deptCode": {
"*": {
// 保留包含 deptCode 和 deptName 的对象
"@2": "operatingRoomList[]"
}
}
}
}
}
}
]
3.9 数组转换逗号拼接
比如发送短信入参是数组,需要转换成逗号拼接
输入input
{
"smsContent": "hello zhangsan",
"telephoneNumberList": [
"13858094127",
"13858094133"
]
}
json spec
[
{
"operation": "shift",
"spec": {
"telephoneNumberList": {
"*": "aimcodes[]"
},
"smsContent": "content"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"aimcodes": "=join(',',@(1,aimcodes))"
}
},
{
"operation": "remove",
"spec": {
"aimcodes": {
"*": ""
}
}
}
]
3.10 省市区县拼接
需要判断输入数据 “Type”="H"的时候,拼装省市区县街道的数据 到Address 字段
输入input
{
"AddressList": [
{
"Zip": "310002",
"StreetAddress": "西湖2道",
"Type": "H",
"Town": {
"Identifier": "330102001",
"Text": "清波街道"
},
"City": {
"Identifier": "330100",
"Text": "杭州市"
},
"County": {
"Identifier": "330102",
"Text": "上城区"
},
"Province": {
"Identifier": "330000",
"Text": "浙江省"
}
},
{
"StreetAddress": "111",
"Type": "C",
"Town": {
"Identifier": "330102001",
"Text": "清波街道"
},
"City": {
"Identifier": "330100",
"Text": "杭州市"
},
"County": {
"Identifier": "330102",
"Text": "上城区"
},
"Province": {
"Identifier": "330000",
"Text": "浙江省"
}
}
]
}
JSON JOLT
[
{
"operation": "shift",
"spec": {
"AddressList": {
"*": {
"Type": {
"H": {
"@(2,StreetAddress)": "StreetAddress",
"@(2,City.Text)": "City",
"@(2,County.Text)": "County",
"@(2,Town.Text)": "Town",
"@(2,Province.Text)": "Province"
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
//拼装数据
"Address": "=concat(@(1,Province), @(1,City), @(1,County), @(1,Town), @(1,StreetAddress))"
}
},
{
"operation": "remove",
"spec": {
// 指定要移除的字段的路径
"StreetAddress": "",
"City": "",
"County": "",
"Town": "",
"Province": ""
}
}
]
3.11 省市区县拼接平铺结果
确保当某个字段为空时,不会被拼接
输入json
{
"addrProvinceCodeDesc": "四川省",
"addrCityCodeDesc": "成都市",
"addrCountyCodeDesc": "高新区",
"addrTownCodeDesc": "石羊街道",
"addrDetail": "天府新谷"
}
JSON JOLT spec
[
{
"operation": "shift",
"spec": {
"addrProvinceCodeDesc": "addrProvinceCodeDesc",
"addrCityCodeDesc": "addrCityCodeDesc",
"addrCountyCodeDesc": "addrCountyCodeDesc",
"addrTownCodeDesc": "addrTownCodeDesc",
"addrDetail": "addrDetail",
"*": "&"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"addrProvinceCodeDesc": "=if(@(1,addrProvinceCodeDesc), @(1,addrProvinceCodeDesc), '')",
"addrCityCodeDesc": "=if(@(1,addrCityCodeDesc), @(1,addrCityCodeDesc), '')",
"addrCountyCodeDesc": "=if(@(1,addrCountyCodeDesc), @(1,addrCountyCodeDesc), '')",
"addrTownCodeDesc": "=if(@(1,addrTownCodeDesc), @(1,addrTownCodeDesc), '')",
"addrDetail": "=if(@(1,addrDetail), @(1,addrDetail), '')",
"Address": "=concat(@(1,addrProvinceCodeDesc), @(1,addrCityCodeDesc), @(1,addrCountyCodeDesc), @(1,addrTownCodeDesc), @(1,addrDetail))"
}
},
{
"operation": "remove",
"spec": {
"addrProvinceCodeDesc": "",
"addrCityCodeDesc": "",
"addrCountyCodeDesc": "",
"addrTownCodeDesc": "",
"addrDetail": ""
}
}
]