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

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

JOLT Transformation and add a new array to the output

问题

Here is the translated content you requested:

{
  "data": {
    "tests": [
      {
        "TestID": 304674,
        "Name": "Test Number One",
        "Description": "First Test",
        "locations": [
          "Paris",
          "London"
        ]
      },
      {
        "id": 12345,
        "name": "Test Number Two",
        "description": "Second test",
        "locations": [
          "Paris",
          "London"
        ]
      }
    ]
  }
}

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:

{
  "data": {
    "tests": [
      {
        "id": 304674,
        "name": "Test Number One",
        "description": "First Test"
      },
      {
        "id": 12345,
        "name": "Test Number Two",
        "description": "Second test"
      }
    ]
  }
}

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

{
  "data": {
    "tests": [
      {
        "TestID": 304674,
        "Name": "Test Number One",
        "Description": "First Test",
        "locations": [
          "Paris",
          "London"
        ],
      },
      {
        "id": 12345,
        "name": "Test Number Two",
        "description": "Second test",
        "locations": [
          "Paris",
          "London"
        ],
      }
    ]
  }
}

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

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "tests": {
          "*": {
            "id": "data.tests[&1].TestID",
            "name": "data.tests[&1].Name",
            "description": "data.tests[&1].Description"
          }
        }
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "data": {
        "tests": {
          "*": {
            "locations": ["Paris", "London"]
          }
        }
      }
    }
  }
]

答案1

得分: 1

Here's the translated content:

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

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

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

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "tests": {
          "*": {
            "id": "&3.&2[&1].TestID",
            "n*": "&3.&2[&1].N&(0,1)",
            "d*": "&3.&2[&1].D&(0,1)",
            "#Paris|#London": "&3.&2[&1].locations"
          }
        }
      }
    }
  }
]
英文:

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:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "tests": {
          "*": {
            "id": "&3.&2[&1].TestID",
            "n*": "&3.&2[&1].N&(0,1)",
            "d*": "&3.&2[&1].D&(0,1)",
            "#Paris|#London": "&3.&2[&1].locations"
          }
        }
      }
    }
  }
]

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:

确定