Chethan
Chethan

Reputation: 73

Jolt Conversion need to convert list of object to id value pairs with comma separated

Can someone help to get joltspec in required format.

Input request:

{
  "data": {
    "testDetails": [
      {
        "contactId": "941",
        "contactType": "DM"
      },
      {
        "contactId": "1041",
        "contactType": "CM"
      }
    ]
  }
}

Expected output:

{
  "id": "test_details",
  "list": [
    {
      "id": "contact_type",
      "value": "DM,CM"
    },
    {
      "id": "contact_id",
      "value": "941,1041"
    }
  ]
}

InputSpec:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "#test_details": "Request.dataset.testDetails.param.[0].id",
        "@testDetails": "Request.dataset.testDetails.param.[0].list"
      }
    }
  }
]

If i try like this, I am getting same list as it is in input. I have tried using shift but could not make it to get as id value pair which is expected. Can anyone help on this

Upvotes: 2

Views: 228

Answers (2)

kasptom
kasptom

Reputation: 2458

If leaving the camel case instead of the snake case for the ouptut is not a problem, this spec:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "testDetails": {
          "*": {
            "*": "&.values"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "&.id",
        "*": "&1.&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "list.[#1]",
      "#test_details": "id"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "list": {
        "*": {
          "values": "=join(',',@(1,values))"
        }
      }
    }
  }
]

produces the output like below:

{
  "id" : "test_details",
  "list" : [ {
    "id" : "contactId",
    "values" : "941, 1041"
  }, {
    "id" : "contactType",
    "values" : "DM, CM"
  } ]
}

Try to examine the output by gradually adding the "shift" operations

Also, have a look at the javadocs of the shift operation and the join function example


edit: if you insist on the snake case, add this shift operation at the beginning of the spec:

{
  "operation": "shift",
  "spec": {
    "data": {
      "testDetails": {
        "*": {
          "contactId": "&3.&2[#2].contact_id",
          "contactType": "&3.&2[#2].concact_type"
        }
      }
    }
  }
}

Upvotes: 1

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

You can figure out to combine those comma seperated values through use of modify-overwrite-beta operation, and apply shift for the rest such as

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "testDetails": {
          "*": {
            "*": "&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "&.id",
        "*": "&1.&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "value": "=concat(@(1,0),',',@(1,1))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@(1,&.id)": "&.id",
        "@(1,&.value)": "&.value"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "#id": "test_details",
      "*": "list"
    }
  }
]

Upvotes: 1

Related Questions