json Jolt变换块平面阵列为两个一对

nkcskrwz  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(124)

我试图将两个对分组在一个平面列表中,注意对的数量可以是可变的(例如0,2,4,6等)。
参见预期输入/输出。
输入:

{
  "coordinates": [
    1.1,
    5.1,
    1.2,
    5.3,
    1.3,
    5.5
  ]
}

输出:

{
  "coordinates": [
    [1.1, 5.1],
    [1.2, 5.3],
    [1.3, 5.5],  
  ]
}

使用Jolt转换可以轻松实现这一点吗?

8cdiaqws

8cdiaqws1#

是的,您可以通过使用连续的转换(如

[
 //index each values seperately
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.&"
      }
    }
  },
 // convert values to string type so as to prevent the issue of truncation of decimal parts of those values
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=toString(@(1,&))"
      }
    }
  },
 // exchange key-value pairs
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$": "&2.@(0)"
        }
      }
    }
  },
  {
 // increment the values by 1 in order to prepare them for modular arithetic logic held in the following steps
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=intSum(@(1,&),1)"
      }
    }
  },
  {
 // pairs means to have two components, then need to divide the values by 2 along with rounding to the nearest grater integer
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=divideAndRound(0,@(1,&),2)"
      }
    }
  },
  {
  // exchange key-value pairs back while keeping the name of the object("coordinates")
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$": "&2.@(0)"
        }
      }
    }
  },
  {
 // dissipate each component of the list to their proper place
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1[]"
      }
    }
  }
]

相关问题