Reputation: 101
I am trying to find the solution for transforming the below JSON data. Mainly I want the date
which is having /
to be replaced with -
.
The replace logic which I tried below is not working. Can someone help me in this?
It seems like replace with modify-overwrite-beta is not working.
Input JSON
{
"certificates": [
{
"name": "name1",
"description": "description1",
"mapping": [
{
"date": "DEC/2023",
"code": "ABC1",
"rating": null,
"scope": [
"scope1",
"scope2",
"scope3"
]
}
]
},
{
"name": "name2",
"description": "description2",
"mapping": [
{
"date": null,
"code": "ABC1",
"rating": null,
"scope": [
"TBD"
]
}
]
}
]
}
Expected output:
{
"certificates": [
{
"name": "name1",
"description": "description1",
"validity": "DEC-2023",
"code": "ABC1",
"rating": null,
"scope": [
"scope1",
"scope2",
"scope3"
]
},
{
"name": "name2",
"description": "description2",
"validity": null,
"code": "ABC1",
"rating": null,
"scope": [
"TBD"
]
}
],
"code": "ABC1"
}
Tried the below spec
[
{
"operation": "shift",
"spec": {
"certificates": {
"*": {
"name": "certificates[&1].name",
"description": "certificates[&1].description",
"mapping": {
"*": {
"*": "certificates[&3].&",
"date": "certificates[&3].validity"
}
}
}
}
}
},
{
"operation": "default",
"spec": {
"code": "ABC1"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"certificates": {
"*": {
"validity": "=replace(validity,'/','-')"
}
}
}
}
]
Getting below output
Upvotes: 1
Views: 63
Reputation: 65408
Yet, you can use modify-overwrite-beta transformation along with split and join functions such as :
[
{
"operation": "modify-overwrite-beta",
"spec": {
"certificates": {
"*": {
"mapping": {
"*": { //handle multiple characters(i.e."/","-"..etc.) at once
"date": "=split('/',@(1,&))",
"validity": "=join('-',@(1,date))"
}
}
}
}
}
},
{ // get rid of the original "date" attribute
"operation": "remove",
"spec": {
"certificates": {
"*": {
"mapping": {
"*": {
"date": ""
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"@": "", //current value of the whole JSON
"@certificates[0].mapping[0].code": "code" //add an extra attribute
//without an hardcode
}
}
]
the demo on the site https://jolt-demo.appspot.com/ is :
Upvotes: 1
Reputation: 495
Hope the below Jolt Spec will be helpful for you.
[
{
"operation": "modify-default-beta",
"spec": {
"certificates": {
"*": {
"mapping": {
"*": {
"isDateNotNull": ["=notNull(@(1,date))", false],
"splitDate": "=split('/',@(1,date))",
"customDate": "=concat(@(1,splitDate[0]), '-',@(1,splitDate[1]) )"
}
}
}
},
"code": "ABC1"
}
},
{
"operation": "shift",
"spec": {
"certificates": {
"*": {
"*": "certificates[&1].&",
"mapping": {
"*": {
"isDateNotNull": {
"false": {
"@(2,date)": "certificates[&5].validity"
},
"*": {
"@(2,customDate)": "certificates[&5].validity"
}
},
"*": "certificates[&3].&"
}
}
}
},
"*": "&"
}
},
{
"operation": "remove",
"spec": {
"certificates": {
"*": {
"date": "",
"customDate": "",
"splitDate": ""
}
}
}
}
]
Upvotes: 1