'How to replace text for some properties using jolt
I'm using jolt in order to do some transformations, I've been able to do that but I'm having some issues trying to replace some text in some properties of my JSON file.
I tried to split the value by / and then concat the part I need with an additional text, unfortunately, that's not working.
Input JSON
{
"components": {
"values": {
"value1": {
"$path": "1/2/3/bear"
},
"value2": {
"$path": "1/2/3/cat"
},
"value3": {
"$path": "1/2/3/lion"
}
}
}
}
I want to add '#/myvalue/' to each $path value using the last part of it.
Expected result
{
"components": {
"values": {
"value1": {
"$path": "#/myvalue/bear"
},
"value2": {
"$path": "#/myvalue/cat"
},
"value3": {
"$path": "#/myvalue/lion"
}
}
}
}
I tried the following one approach in order to take the value but it's not working as expected.
[
{
"operation": "shift",
"spec": {
"components": {
"values": {
"*": {
"\\$path": { //This key has de $ sign
"*/*/*/*": {
"$(0,4)": "\\$path" //I need to take the four part and assign that to the \\path value
}
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"components": {
"values": {
"*": {
"\\$path": "=concat('#/myvalue/', @(1,\\$path))"
}
}
}
}
}
]
Solution 1:[1]
All you need is to manipulate the original path string using some functions jolt provides (like split, last element, concat etc)
This spec will do the trick:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"components": {
"values": {
"*": {
"temp": "=split('/',@(1,\\$path))",
"last_element": "=lastElement(@(1,temp))",
"\\$path": "=concat(#/myvalue/,@(1,last_element))"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"components": {
"values": {
"*": {
"\\$path": "components.values.&1.&"
}
}
}
}
}
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Barbaros Özhan |