skip to Main Content

I have this collection of driver details and I want to use jolt to copy the properties to concatenate first name and last name within a single property called name

{
  "drivers_details": [
    {
      "first_name": "Doru",
      "last_name": "Petre",
      "cnp": "1641201390687",
      "id_series": "RK",
      "id_number": "123456"
    },
    {
      "first_name": "GIGI",
      "last_name": "FANE",
      "cnp": "16412013906871",
      "id_series": "RK",
      "id_number": "1234567"
    }
  ]
}

I am using this spec for the shift transformation spec

[
  {
    "operation": "shift",
    "spec": {
      "drivers_details": {
        "*": {
          "cnp": "body.drivers[&1].tin",
          "id_series": "body.drivers[&1].id_series",
          "id_number": "body.drivers[&1].id_number"
        }
      }
    }
  }
]

and this one for modify-overwrite, but it doesn’t work how I was expecting

 {
    "operation": "modify-overwrite-beta",
    "spec": {
      "drivers_details": {
        "*": {
           "name": "=concat(@(2,first_name),' ',@(2,last_name))"
        }
      }
    }
  }

What is the correct way of doing this?

2

Answers


  1. You can use modify transformation as in the following case, and get rid of names other than newly formed attribute name such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "drivers_details": {
            "*": {
              "name": "=concat(@(1,first_name),' ',@(1,last_name))"
            }
          }
        }
      },
      {
        "operation": "remove",
        "spec": {
          "drivers_details": {
            "*": {
              "*name": ""
            }
          }
        }
      }, 
      { // this last spec stands only for ordering of the attributes if the ordering and putting the "name" at the top matters.
        // So, you might get rid of the "remove" transformation above if this is used. Otherwise keep it.
        "operation": "shift",
        "spec": {
          "drivers_details": {
            "*": {
              "name": "&2[&1].&",
              "cnp": "&2[&1].&",
              "id*": "&2[&1].&"
            }
          }
        }
      }
    ]
    

    where those attributes(first/last_name) stays right-hand-side at the current level, so just only need to traverse 1 colon(:) which appears as @(1,..)

    Login or Signup to reply.
  2. You can use this spec without the remove operation:

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "name": "=concat(@(1,first_&),' ',@(1,last_&))"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "name": "&2[&1].&",
              "cnp": "&2[&1].&",
              "id_*": "&2[&1].&"
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search