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?
2
Answers
For the general case where
extraFields
resides as a key within a larger Json structure for instance, there's another keyname
at the same level i.e.Aiming to keep all other fields intact and solely tweak
extraFields
.The corresponding Jolt would be:
Credit to Barbaros Özhan
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