I have the following inputted Json:
{
"extraFields": {
"language": "french",
"status": {
"comment": "hey"
}
}
}
Using Jolt, I want to achieve the following output:
{
"extraFields": "{\"language\": \"french\", \"status\": {\"comment\": \"hey\"}}"
}
Note the backslashes.
I managed to do that with the following Jolt formula:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"extraFields": "=concat('{', '\"language\": ', '\"', @(0,language), '\" ,', '\"status\": ', '{', '\"comment\": ', '\"', @(1,extraFields.status.comment), '\"', '}', '}')"
}
}
]
Problem is, fields of extraFields
are optional meaning, some of them might not be inputted.
For example, status
can be omitted or language
or both.
It results, empty values of non-inputted keys.
Is it possible to concatenate only inputted keys?
You can pad the related characters by using a shift, then stringify the attribute by a modify transformation through use of toString
function such as
[
{
"operation": "shift",
"spec": {
"*": {
"language": {
"*": "&2.\"&1\".\"&\""
},
"status": {
"*": {
"*": "&3.\"&2\".\"&1\".\"&\""
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*language*": {
"*": {
"$": "&3.&2"
}
},
"*status*": {
"*": {
"*": {
"$": "&4.&3.&2"
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=toString"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"temp1": "=toString(@(1,extraFields))",
"temp2": "=split('=',@(1,temp1))",
"extraFields": "=join(':',@(1,temp2))"
}
},
{
"operation": "remove",
"spec": {
"temp*": ""
}
}
]