将数组转换为多个对象

huangapple go评论85阅读模式
英文:

Jolt transform array into multiple Objects

问题

I am trying to transform the below JSON:

Input:

  1. {
  2. "steps": [
  3. {
  4. "end": "2023-01-27T09:19:29.849298Z",
  5. "id": "1",
  6. "start": "2023-01-27T09:18:24.59325Z",
  7. "name": "foo"
  8. },
  9. {
  10. "end": "2023-01-28T09:19:29.849298Z",
  11. "id": "2",
  12. "start": "2023-01-28T09:18:24.59325Z",
  13. "name": "bar"
  14. }
  15. ]
  16. }

Output:

  1. {
  2. "steps": [
  3. {
  4. "end": "2023-01-27T09:19:29.849298Z",
  5. "id": "1",
  6. "name": "foo",
  7. "start": "2023-01-27T09:18:24.59325Z"
  8. },
  9. {
  10. "end": "2023-01-28T09:19:29.849298Z",
  11. "id": "2",
  12. "name": "bar",
  13. "start": "2023-01-28T09:18:24.59325Z"
  14. }
  15. ],
  16. "date": [
  17. {
  18. "name": "startDate",
  19. "value": "2023-01-27T09:18:24.59325Z" //steps[0].start
  20. },
  21. {
  22. "name": "endDate",
  23. "value": "2023-01-27T09:19:29.849298Z" //steps[0].end
  24. }
  25. ]
  26. }

I tried using the below spec:

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "steps": {
  6. "*": "steps[]",
  7. "0": {
  8. "#startDate": "date[0].name",
  9. "start": "date[0].value",
  10. "end": "date[1].value",
  11. "#endDate": "date[1].name"
  12. }
  13. }
  14. }
  15. }
  16. ]

But "*": "steps[]" only transforms the last element of the array steps. Please guide me as to what is wrong with the above spec, as I am new to jolt. Also, any pointers to the correct operations needed to achieve the above output will be greatly appreciated.

英文:

I am trying to transform the below JSON:

Input:

  1. {
  2. "steps": [
  3. {
  4. "end": "2023-01-27T09:19:29.849298Z",
  5. "id": "1",
  6. "start": "2023-01-27T09:18:24.59325Z",
  7. "name": "foo"
  8. },
  9. {
  10. "end": "2023-01-28T09:19:29.849298Z",
  11. "id": "2",
  12. "start": "2023-01-28T09:18:24.59325Z",
  13. "name": "bar"
  14. }
  15. ]
  16. }

Output:

  1. {
  2. "steps": [
  3. {
  4. "end": "2023-01-27T09:19:29.849298Z",
  5. "id": "1",
  6. "name": "foo",
  7. "start": "2023-01-27T09:18:24.59325Z"
  8. },
  9. {
  10. "end": "2023-01-28T09:19:29.849298Z",
  11. "id": "2",
  12. "name": "bar",
  13. "start": "2023-01-28T09:18:24.59325Z"
  14. }
  15. ],
  16. "date": [
  17. {
  18. "name": "startDate",
  19. "value": "2023-01-27T09:18:24.59325Z" //steps[0].start
  20. },
  21. {
  22. "name": "endDate",
  23. "value": "2023-01-27T09:19:29.849298Z" //steps[0].end
  24. }
  25. ]
  26. }

I tried using the below spec:

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "steps": {
  6. "*": "steps[]",
  7. "0": {
  8. "#startDate": "date[0].name",
  9. "start": "date[0].value",
  10. "end": "date[1].value",
  11. "#endDate": "date[1].name"
  12. }
  13. }
  14. }
  15. }
  16. ]

But "*": "steps[]" only transforms the last element of the array steps. Please guide me as to what is wrong with the above spec, as I am new to jolt. Also, any pointers to the correct operations needed to achieve the above output will be greatly appreciated.

答案1

得分: 1

你可以使用以下规范:

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "*": {
  6. "@": "&1",
  7. "0": {
  8. "#startDate": "date[0].name",
  9. "start": "date[0].value",
  10. "#endDate": "date[1].name",
  11. "end": "date[1].value"
  12. }
  13. }
  14. }
  15. }
  16. ]

你可以使用@来获取steps数组,并将其放入steps键中,使用&1

英文:

You can use this spec:

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "*": {
  6. "@": "&1",
  7. "0": {
  8. "#startDate": "date[0].name",
  9. "start": "date[0].value",
  10. "#endDate": "date[1].name",
  11. "end": "date[1].value"
  12. }
  13. }
  14. }
  15. }
  16. ]

You can use @ to get the steps array and put it into the steps key with &1.

答案2

得分: 0

以下是翻译好的部分:

One option is to conditionally pick by the existing(steps) array's indexes while walking through it such as

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "steps": {
  6. "@1": "", // 从上一层派生整个值,例如复制它
  7. "0": { // 第一个索引
  8. "#startDate": "date[&1].name",
  9. "start": "date[&1].value"
  10. },
  11. "*": { // 其他索引(在这种情况下只有一个)
  12. "#endDate": "date[&1].name", // 使用&1(向上移动一级树)提取包装对象的索引值
  13. "end": "date[&1].value"
  14. }
  15. }
  16. }
  17. }
  18. ]

the demo on the site http://jolt-demo.appspot.com/ is
将数组转换为多个对象
1: https://i.stack.imgur.com/C8p5h.jpg

英文:

One option is to conditionally pick by the existing(steps) array's indexes while walking through it such as

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "steps": {
  6. "@1": "",// derive the whole value from the upper level, eg. replicate it
  7. "0": {// the first index
  8. "#startDate": "date[&1].name",
  9. "start": "date[&1].value"
  10. },
  11. "*": {// the other index(this case there's only one)
  12. "#endDate": "date[&1].name",// bring the value of the wrapper object'S index by using &1(going up one level the tree)
  13. "end": "date[&1].value"
  14. }
  15. }
  16. }
  17. }
  18. ]

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

将数组转换为多个对象

huangapple
  • 本文由 发表于 2023年2月6日 21:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定