JOLT转换并将新数组添加到输出。

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

JOLT Transformation and add a new array to the output

问题

Here is the translated content you requested:

  1. {
  2. "data": {
  3. "tests": [
  4. {
  5. "TestID": 304674,
  6. "Name": "Test Number One",
  7. "Description": "First Test",
  8. "locations": [
  9. "Paris",
  10. "London"
  11. ]
  12. },
  13. {
  14. "id": 12345,
  15. "name": "Test Number Two",
  16. "description": "Second test",
  17. "locations": [
  18. "Paris",
  19. "London"
  20. ]
  21. }
  22. ]
  23. }
  24. }

Please note that I've removed the HTML entity codes (e.g., ") to provide you with the translated JSON content.

英文:

I have the following JSON input that I would like to transform using JOLT:

  1. {
  2. "data": {
  3. "tests": [
  4. {
  5. "id": 304674,
  6. "name": "Test Number One",
  7. "description": "First Test"
  8. },
  9. {
  10. "id": 12345,
  11. "name": "Test Number Two",
  12. "description": "Second test"
  13. }
  14. ]
  15. }
  16. }

I would like the output to rename some things and then add a new array called locations, resulting in the following desired output:

  1. {
  2. "data": {
  3. "tests": [
  4. {
  5. "TestID": 304674,
  6. "Name": "Test Number One",
  7. "Description": "First Test",
  8. "locations": [
  9. "Paris",
  10. "London"
  11. ],
  12. },
  13. {
  14. "id": 12345,
  15. "name": "Test Number Two",
  16. "description": "Second test",
  17. "locations": [
  18. "Paris",
  19. "London"
  20. ],
  21. }
  22. ]
  23. }
  24. }

I have tried the following JOLT spec on apache-nifi but when I run it I only get "Paris" for locations.

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "data": {
  6. "tests": {
  7. "*": {
  8. "id": "data.tests[&1].TestID",
  9. "name": "data.tests[&1].Name",
  10. "description": "data.tests[&1].Description"
  11. }
  12. }
  13. }
  14. }
  15. },
  16. {
  17. "operation": "modify-default-beta",
  18. "spec": {
  19. "data": {
  20. "tests": {
  21. "*": {
  22. "locations": ["Paris", "London"]
  23. }
  24. }
  25. }
  26. }
  27. }
  28. ]

答案1

得分: 1

Here's the translated content:

到目前为止,尝试还不错。我可以补充以下观点:

  • 无需重复文字,而是使用符号表示法,如**&2**(例如:上溯两级树并抓取文字 tests),&3data
  • &(0,1) 代表来自当前级别(0th)的星号的第1次出现
  • "locations": ["Paris", "London"] 可能直接添加到位移转换中(而不需要额外的转换),同时重命名当前对象的属性

因此,您可以使用以下规范:

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "data": {
  6. "tests": {
  7. "*": {
  8. "id": "&3.&2[&1].TestID",
  9. "n*": "&3.&2[&1].N&(0,1)",
  10. "d*": "&3.&2[&1].D&(0,1)",
  11. "#Paris|#London": "&3.&2[&1].locations"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. ]
英文:

So far so good attempt. I can add the following points of view :

  • no need to repeat the literals but use ampersand notations such as &2(eg.:going two levels up the tree and grabbing the literal tests), &3(data)
  • &(0,1) to represent the 1st occurence of the asterisk from the levelcurrent(0th)
  • "locations": ["Paris", "London"] might be directly added within the shift transformation(without needing an extra transformation) while renaming the attributes for the current objects

So, you can use the following spec:

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "data": {
  6. "tests": {
  7. "*": {
  8. "id": "&3.&2[&1].TestID",
  9. "n*": "&3.&2[&1].N&(0,1)",
  10. "d*": "&3.&2[&1].D&(0,1)",
  11. "#Paris|#London": "&3.&2[&1].locations"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. ]

huangapple
  • 本文由 发表于 2023年6月9日 07:00:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436207.html
匿名

发表评论

匿名网友

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

确定