英文:
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
),&3(data
) - &(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"
}
}
}
}
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论