skip to Main Content

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

enter image description here

2

Answers


  1. 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": ""
            }
          }
        }
      }
    ]
    
    
    Login or Signup to reply.
  2. Yet, you can use modify-overwrite-beta transformation along with split and join functions such as :

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "certificates": {
            "*": {
              "mapping": {
                "*": {
                  "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
        }
      }
    ]
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search