Home > Net >  Modify a JSON using Jolt Transform
Modify a JSON using Jolt Transform

Time:10-06

I have an array of JSONs as listed below:

[
  {
    "id": 1
  },
  {
    "id": 2
  },
  {
    "id": 3
  }
]

The following are the objectives:

(1) Modify the list above into:

{
  "data": [
    {
      "id": 1
    },
    {
      "id": 2
    },
    {
      "id": 3
    }
  ],
  "idList": [ 1, 2, 3 ]
}

(2) Calculate the Minimum and Maximum of "idList" to finally obtain:

{
  "data": [
    {
      "id": 1
    },
    {
      "id": 2
    },
    {
      "id": 3
    }
  ],
    "minID": 1,
    "maxID": 3,
}

I think (2) is straightforward after getting (1), as I can simply use:

min(@(1,idList))

I have a problem in converting the original input into (1), here's my attempt:

[
  {
    "operation": "shift",
    "spec": {
      "*": "data"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": { "*": { "id": "idList" } }
    }
  }
]

which yields:

{
  "idList" : [ 1, 2, 5 ]
}

Can anyone help on this? Also, am a newbie to this Jolt Transform technique, can anyone suggest a good source for mastering this ? (like a book)

CodePudding user response:

You can consecutively use a shift, and modify transformation specs such as

[
  {
    // for the first objective
    "operation": "shift",
    "spec": {
      "*": {
        "@": "data",
        "*": "idList"
      }
    }
  },
  {
    // for the second one
    "operation": "modify-overwrite-beta",
    "spec": {
      "minID": "=min(@(1,idList))",
      "maxID": "=max(@(1,idList))"
    }
  }
]

the demo on the site enter image description here

  • Related