Hi working in Jolt transformation tool and getting know some in-depth details Input
{
"characteristic": [
{
"name": "usageCharacteristicName",
"value": "availableBalance",
"@type": "usageCharacteristic",
"arrayIndex": "2"
},
{
"name": "usageCharacteristicValue",
"value": "2999.25",
"@type": "usageCharacteristic",
"arrayIndex": "2"
},
{
"name": "usageCharacteristicName",
"value": "Name",
"@type": "usageCharacteristic",
"arrayIndex": "0"
},
{
"name": "usageCharacteristicValue",
"value": "Jack",
"@type": "usageCharacteristic",
"arrayIndex": "0"
}
{
"name": "usageCharacteristicName",
"value": "Likes",
"@type": "usageCharacteristic",
"arrayIndex": "1"
},
{
"name": "usageCharacteristicValue",
"value": "Code",
"@type": "usageCharacteristic",
"arrayIndex": "1"
}
]
}
Jolt Spec :
[
{
"operation": "shift",
"spec": {
"characteristic": {
"*": {
"arrayIndex": {
"2": {
"@(2,value)": "data.queryBalance.accountBalance"
},
"0": {
"@(2,value)": "data.queryBalance.Name"
},
"1": {
"@(2,value)": "data.queryBalance.Likes"
},
}
}
}
}
}
]
Output :
{
"data" : {
"queryBalance" : {
"accountBalance" : [ "availableBalance", "2999.25" ],
"Name" : [ "Name", "Jack" ],
"Likes" : [ "Likes", "Code" ]
}
}
}
the Output i was getting is not good enough for me, want to do like String like bellow Expected.
Expected Output : "availableBalance" : "2999.25", "Name" : "Jack", "Likes" : "Code"
how do i get like a String ?
CodePudding user response:
You need a modify transformation spec as well such as
[
{
// reduce to simple array "value" : [ "availableBalance", "2999.25" ] nested within an object
"operation": "shift",
"spec": {
"*": {
"*": {
"value": "&"
}
}
}
},
{
// combine components of the array so as to reform as a string
"operation": "modify-overwrite-beta",
"spec": {
"*": "=join(' : ',@(1,&))"
}
}
]
CodePudding user response:
I know how to do it by another library Josson. You may consider to use it.
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(inputJSON);
Transform to an array of string
JsonNode node = josson.getNode(
"characteristic"
".group(arrayIndex)"
".concat(elements[name='usageCharacteristicName'].value"
" ,' : '"
" ,elements[name='usageCharacteristicValue'].value)");
System.out.println(node.toPrettyString());
Output
[ "availableBalance : 2999.25", "Name : Jack", "Likes : Code" ]
Transform to a single string
String str = josson.getString(
"characteristic"
".group(arrayIndex)"
".concat(elements[name='usageCharacteristicName'].value"
" ,' : '"
" ,elements[name='usageCharacteristicValue'].value)"
".join(', ')");
System.out.println(str);
Output
availableBalance : 2999.25, Name : Jack, Likes : Code
CodePudding user response:
I think this solve the problem spec
[
{
//group keys and valeus for arrayIndex
"operation": "shift",
"spec": {
"characteristic": {
"*": {
"value": "newArray[@(1,arrayIndex)].@(1,name)"
}
}
}
},
{
//create a new object use a key and valeus matchs previous
"operation": "shift",
"spec": {
"newArray": {
"*": {
"usageCharacteristicValue": "@(1,usageCharacteristicName)"
}
}
}
}
]
output
{
"Name" : "Jack",
"Likes" : "Code",
"availableBalance" : "2999.25"
}