I am doing conversion of string to integer/double in my jolt spec. If elements does not come in request, then empty object is coming in response. Instead I do not want to pass that object itself as its empty.
Input Request Working:
{
"data": {
"first": "1",
"second": "2"
}
}
Jolt Spec:
[
{
"operation": "shift",
"spec": {
"data": {
"first": "datas.firstTag",
"second": "datas.second.secondTag"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"datas": {
"firstTag": "=toInteger",
"second": {
"secondTag": "=toInteger"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
}
]
Output if tag is there in request:
{
"datas" : {
"firstTag" : 1,
"second" : {
"secondTag" : 2
}
}
}
But when input request is like below where I do not get second tag:
{
"data": {
"first": "1"
}
}
Current Output:
{
"datas" : {
"firstTag" : 1,
"second" : { }
}
}
Getting second object as empty
"second" : { }
Expected Output:
{
"datas" : {
"firstTag" : 1
}
}
Please help to fix this issue.
Advertisement
Answer
Rethink in a dynamical manner by using ampersand placeholders such as
[
{
"operation": "shift",
"spec": {
"*": {
"first": "&1s.&Tag",
"*": { "@(1,&)": "&2s.&.&Tag" }
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"firstTag": "=toInteger",
"*": {
"*": "=toInteger"
}
}
}
}
]
the suffixed integers for those ampersands represents the level to go up to grab the desired value such as &1 and &2 stand for data, & without integer suffixes represent the value from the current level.
“*” wildcard represents the rest of the elements, other than first in this case(you might have third, fourth … as other elements to be formatted like the second)
Edit : For the current case which’s mentioned within the edit in order to get
{
"datas" : {
"firstTag" : "1",
"second" : {
"secondTag" : "2",
"secondNew" : "3"
}
}
}
from the input
{
"data": {
"first": "1",
"second": "2",
"secondNew": "3"
}
}
which had a new entry “secondNew”: “3”, you’ll need to write each key explicitly within the spec such as
[
{
"operation": "shift",
"spec": {
"*": {
"first": "&1s.&Tag",
"second": "&1s.&.&Tag",
"secondNew": "&1s.second.&"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"firstTag": "=toInteger",
"*": {
"*": "=toInteger"
}
}
}
}
]
in order to combine the rest of the elements under common object.